package com.jeesharp.business.modules.food.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeesharp.business.modules.food.entity.FoodCompanyAccountLog;
import com.jeesharp.business.modules.food.mapper.FoodCompanyAccountLogMapper;
import com.jeesharp.business.modules.food.service.FoodCompanyAccountLogService;
import com.jeesharp.business.modules.food.vo.FoodCompanyAccountLogQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.utils.WebFrameworkUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class FoodCompanyAccountLogServiceImpl extends CrudService<FoodCompanyAccountLogMapper, FoodCompanyAccountLog, FoodCompanyAccountLogQueryVO> implements FoodCompanyAccountLogService {

    private static final String DEL_NORMAL = "0";

    @Override
    @Transactional(readOnly = false)
    public void insertLog(FoodCompanyAccountLog log) {
        log.setIsNewRecord(true);
        log.preInsert();
        getBaseMapper().insert(log);
    }

    @Override
    public Page<FoodCompanyAccountLog> findLogPage(Page<FoodCompanyAccountLog> page, FoodCompanyAccountLogQueryVO query) {
        FoodCompanyAccountLogQueryVO q = query != null ? query : new FoodCompanyAccountLogQueryVO();
        LambdaQueryWrapper<FoodCompanyAccountLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FoodCompanyAccountLog::getDelFlag, DEL_NORMAL);
        wrapper.eq(FoodCompanyAccountLog::getTenantCode, WebFrameworkUtils.getLoginUserTenantCode());
        if (StrUtil.isNotBlank(q.getAccountId())) {
            wrapper.eq(FoodCompanyAccountLog::getAccountId, q.getAccountId().trim());
        }
        if (StrUtil.isNotBlank(q.getCompanyId())) {
            wrapper.eq(FoodCompanyAccountLog::getCompanyId, q.getCompanyId().trim());
        }
        if (StrUtil.isNotBlank(q.getChangeType())) {
            wrapper.eq(FoodCompanyAccountLog::getChangeType, q.getChangeType().trim());
        }
        if (StrUtil.isNotBlank(q.getBizType())) {
            wrapper.eq(FoodCompanyAccountLog::getBizType, q.getBizType().trim());
        }
        if (StrUtil.isNotBlank(q.getBizId())) {
            wrapper.eq(FoodCompanyAccountLog::getBizId, q.getBizId().trim());
        }
        if (StrUtil.isNotBlank(q.getBizNo())) {
            wrapper.eq(FoodCompanyAccountLog::getBizNo, q.getBizNo().trim());
        }
        wrapper.orderByDesc(FoodCompanyAccountLog::getCreateDate);
        page.setPageNo(q.getPageNo());
        page.setPageSize(q.getPageSize());
        page.setCurrent(q.getPageNo());
        page.setSize(q.getPageSize());
        return (Page<FoodCompanyAccountLog>) getBaseMapper().selectPage(page, wrapper);
    }
}
