package cn.superdesk.uniorder.service.invoice.common.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.superdesk.uniorder.common.enums.common.CommonEnabledEnum;
import cn.superdesk.uniorder.common.enums.invoice.common.InvoiceTypeMoudelXModeEnum;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.invoice.common.InvoiceTypeModeConfigSzhtxxPaperInvoice;
import cn.superdesk.uniorder.dao.entity.invoice.common.InvoiceTypeModeConfigSzhtxxPaperInvoiceExample;
import cn.superdesk.uniorder.dao.mapper.invoice.common.InvoiceTypeModeConfigSzhtxxPaperInvoiceMapper;
import cn.superdesk.uniorder.dto.param.invoice.common.InvoiceTypeModeConfigSzhtxxPaperInvoiceParam;
import cn.superdesk.uniorder.dto.param.invoice.common.InvoiceTypeModeConfigSzhtxxPaperInvoiceQueryCriteria;
import cn.superdesk.uniorder.dto.param.invoice.common.InvoiceTypeOpCriteria;
import cn.superdesk.uniorder.service.invoice.common.InvoiceTypeModeConfigSzhtxxPaperInvoiceService;
import cn.superdesk.uniorder.service.invoice.common.InvoiceTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2022-04-22 10:22:57
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022-04-22 10:22:57
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Service
public class InvoiceTypeModeConfigSzhtxxPaperInvoiceServiceImpl implements InvoiceTypeModeConfigSzhtxxPaperInvoiceService {

    @Autowired
    private InvoiceTypeService invoiceTypeService;

    @Autowired
    private InvoiceTypeModeConfigSzhtxxPaperInvoiceMapper invoiceTypeModeConfigSzhtxxPaperInvoiceMapper;


    /****************************************** extends BaseService 部分 start ******************************************/

    @Override
    public int insertSelective(InvoiceTypeModeConfigSzhtxxPaperInvoice entity) {
        return invoiceTypeModeConfigSzhtxxPaperInvoiceMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(InvoiceTypeModeConfigSzhtxxPaperInvoice entity) {
        return invoiceTypeModeConfigSzhtxxPaperInvoiceMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public InvoiceTypeModeConfigSzhtxxPaperInvoice findById(Long id) {
        return invoiceTypeModeConfigSzhtxxPaperInvoiceMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<InvoiceTypeModeConfigSzhtxxPaperInvoice> findByPaging(Map<String, Object> conditions, Integer pageNun, Integer pageSize) {
        return null;
    }

    @Override
    public List<InvoiceTypeModeConfigSzhtxxPaperInvoice> findAll(Map<String, Object> queryMap) {
        return null;
    }

    /****************************************** extends BaseService 部分   end ******************************************/


    public int addInvoiceTypeModeConfigSzhtxxPaperInvoice(InvoiceTypeModeConfigSzhtxxPaperInvoice entity, LoginSysUser loginSysUser) {
        return invoiceTypeModeConfigSzhtxxPaperInvoiceMapper.insertSelective(entity);
    }

    public int updateInvoiceTypeModeConfigSzhtxxPaperInvoice(InvoiceTypeModeConfigSzhtxxPaperInvoice entity, LoginSysUser loginSysUser) {

        int ret = 0;

        InvoiceTypeModeConfigSzhtxxPaperInvoice originEntity = invoiceTypeModeConfigSzhtxxPaperInvoiceMapper.selectByPrimaryKey(entity.getId());

        if (ObjectUtil.isNull(originEntity)) {
            return 0;
        }

        AnBeanUtils.copy(entity, originEntity);

        originEntity.setUpdateTime(DateUtil.date());
        originEntity.setUpdateUserId(loginSysUser.getSysUserId());

        ret = invoiceTypeModeConfigSzhtxxPaperInvoiceMapper.updateByPrimaryKey(originEntity);

        if (ret > 0) {

            if (ObjectUtil.isNull(originEntity.getRuleEnable())) {
                originEntity.setRuleEnable(CommonEnabledEnum.启用.getKey());
            }

            switch (CommonEnabledEnum.get(entity.getRuleEnable())) {
                case 启用:
                    invoiceTypeService.opInvoiceSettingSetEnable(InvoiceTypeOpCriteria
                            .builder()
                            .invoiceTypeId(originEntity.getInvoiceTypeId())
                            .selectByModule(originEntity.getInvoiceTypeModule())
                            .selectByMode(InvoiceTypeMoudelXModeEnum.深圳航信纸质发票.getKey())
                            .build(), loginSysUser);
                    break;
                case 禁用:
                    invoiceTypeService.opInvoiceSettingSetDisable(InvoiceTypeOpCriteria
                            .builder()
                            .invoiceTypeId(originEntity.getInvoiceTypeId())
                            .selectByModule(originEntity.getInvoiceTypeModule())
                            .selectByMode(InvoiceTypeMoudelXModeEnum.深圳航信纸质发票.getKey())
                            .build(), loginSysUser);
                    break;
            }


        }

        return ret;
    }

    public int deleteInvoiceTypeModeConfigSzhtxxPaperInvoice(Long id, LoginSysUser loginSysUser) {
        return invoiceTypeModeConfigSzhtxxPaperInvoiceMapper.deleteByPrimaryKey(id);
    }

    public InvoiceTypeModeConfigSzhtxxPaperInvoice findInvoiceTypeModeConfigSzhtxxPaperInvoiceById(Long id) {
        InvoiceTypeModeConfigSzhtxxPaperInvoice entity = invoiceTypeModeConfigSzhtxxPaperInvoiceMapper.selectByPrimaryKey(id);
//		AssertUtil.notNull(entity);
        return entity;
    }

    public R<List<InvoiceTypeModeConfigSzhtxxPaperInvoice>> pageQuery(Map<String, Object> conditions, int pageNun, int pageSize) {

        InvoiceTypeModeConfigSzhtxxPaperInvoiceExample          example  = new InvoiceTypeModeConfigSzhtxxPaperInvoiceExample();
        InvoiceTypeModeConfigSzhtxxPaperInvoiceExample.Criteria criteria = example.createCriteria();

        // TODO 喜欢用Map 还是 Bean 自己喜欢... 后期添加 转 InvoiceTypeModeConfigSzhtxxPaperInvoiceExample.Criteria
        // InvoiceTypeModeConfigSzhtxxPaperInvoice invoiceTypeModeConfigSzhtxxPaperInvoiceConditions = BeanUtils.mapToBean(conditions,InvoiceTypeModeConfigSzhtxxPaperInvoice.class);

        PageHelper.startPage(pageNun, pageSize);
        List<InvoiceTypeModeConfigSzhtxxPaperInvoice>     resultList = invoiceTypeModeConfigSzhtxxPaperInvoiceMapper.selectByExample(example);
        PageInfo<InvoiceTypeModeConfigSzhtxxPaperInvoice> page       = new PageInfo<InvoiceTypeModeConfigSzhtxxPaperInvoice>(resultList);
        PageHelper.clearPage();

        return new R<List<InvoiceTypeModeConfigSzhtxxPaperInvoice>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), resultList);

    }

    public R<List<InvoiceTypeModeConfigSzhtxxPaperInvoice>> pageQueryGenericParam(InvoiceTypeModeConfigSzhtxxPaperInvoiceQueryCriteria queryCriteria) {

        InvoiceTypeModeConfigSzhtxxPaperInvoiceExample          example  = new InvoiceTypeModeConfigSzhtxxPaperInvoiceExample();
        InvoiceTypeModeConfigSzhtxxPaperInvoiceExample.Criteria criteria = example.createCriteria();

        // TODO 添加条件... 后期添加 queryCriteria 转 InvoiceTypeModeConfigSzhtxxPaperInvoiceExample.Criteria

        PageHelper.startPage(queryCriteria.getPageNumber(), queryCriteria.getPageSize());
        List<InvoiceTypeModeConfigSzhtxxPaperInvoice>     resultList = invoiceTypeModeConfigSzhtxxPaperInvoiceMapper.selectByExample(example);
        PageInfo<InvoiceTypeModeConfigSzhtxxPaperInvoice> page       = new PageInfo<InvoiceTypeModeConfigSzhtxxPaperInvoice>(resultList);
        PageHelper.clearPage();

        return new R<List<InvoiceTypeModeConfigSzhtxxPaperInvoice>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), resultList);

    }

    /****************************************** 操作部分 start ******************************************/

    public InvoiceTypeModeConfigSzhtxxPaperInvoiceParam getByInvoiceTypeOpCriteria(InvoiceTypeOpCriteria opCriteria, LoginSysUser loginSysUser) {

        InvoiceTypeModeConfigSzhtxxPaperInvoiceParam resuleVo = null;

        InvoiceTypeModeConfigSzhtxxPaperInvoiceExample          example  = new InvoiceTypeModeConfigSzhtxxPaperInvoiceExample();
        InvoiceTypeModeConfigSzhtxxPaperInvoiceExample.Criteria criteria = example.createCriteria();

        criteria
                .andInvoiceTypeIdEqualTo(opCriteria.getInvoiceTypeId())
                .andInvoiceTypeModuleEqualTo(opCriteria.getSelectByModule());

        List<InvoiceTypeModeConfigSzhtxxPaperInvoice> resultList = invoiceTypeModeConfigSzhtxxPaperInvoiceMapper.selectByExample(example);

        if (CollUtil.isNotEmpty(resultList)) {
            resuleVo = AnBeanUtils.copy(resultList.get(0), InvoiceTypeModeConfigSzhtxxPaperInvoiceParam.class);
        } else {
            InvoiceTypeModeConfigSzhtxxPaperInvoice entity = new InvoiceTypeModeConfigSzhtxxPaperInvoice();
            entity.setInvoiceTypeId(opCriteria.getInvoiceTypeId());
            entity.setInvoiceTypeModule(opCriteria.getSelectByModule());

            /****************************************** 不能为空的处理 start ******************************************/

            entity.setRuleEnable(CommonEnabledEnum.禁用.getKey());
            entity.setRuleGoodsLines(1999);

            /****************************************** 不能为空的处理   end ******************************************/

            int ret = invoiceTypeModeConfigSzhtxxPaperInvoiceMapper.insertSelective(entity);

            if (ret > 0) {
                resuleVo = AnBeanUtils.copy(entity, InvoiceTypeModeConfigSzhtxxPaperInvoiceParam.class);
            }
        }

        return resuleVo;

    }

    /****************************************** 操作部分   end ******************************************/

}