package cn.superdesk.uniorder.service.accounting.unified.supplier.Impl;

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.numerical.AnCollectionUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.accounting.unified.supplier.AccountingUnifiedSupplierIssues;
import cn.superdesk.uniorder.dao.entity.accounting.unified.supplier.AccountingUnifiedSupplierIssuesExample;
import cn.superdesk.uniorder.dao.mapper.accounting.unified.supplier.AccountingUnifiedSupplierIssuesMapper;
import cn.superdesk.uniorder.dao.mapper.accounting.unified.supplier.AccountingUnifiedSupplierIssuesMapperEx;
import cn.superdesk.uniorder.service.accounting.unified.supplier.AccountingUnifiedSupplierExportExcelService;
import cn.superdesk.uniorder.service.accounting.unified.supplier.AccountingUnifiedSupplierIssuesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
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: 2021-02-24 13:44:11
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-02-24 13:44:11
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class AccountingUnifiedSupplierIssuesServiceImpl implements AccountingUnifiedSupplierIssuesService {

    @Autowired
    private AccountingUnifiedSupplierIssuesMapper       accountingUnifiedSupplierIssuesMapper;
    @Autowired
    private AccountingUnifiedSupplierIssuesMapperEx     accountingUnifiedSupplierIssuesMapperEx;
    @Autowired
    private AccountingUnifiedSupplierExportExcelService accountingUnifiedSupplierExportExcelService;


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


    @Override
    public int insertSelective(AccountingUnifiedSupplierIssues entity) {
        return accountingUnifiedSupplierIssuesMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(AccountingUnifiedSupplierIssues entity) {
        return accountingUnifiedSupplierIssuesMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public AccountingUnifiedSupplierIssues findById(Long id) {
        return accountingUnifiedSupplierIssuesMapper.selectByPrimaryKey(id);
    }

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

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

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


    public void addAccountingUnifiedSupplierIssues(AccountingUnifiedSupplierIssues entity) {
        accountingUnifiedSupplierIssuesMapper.insertSelective(entity);
    }

    public void updateAccountingUnifiedSupplierIssues(AccountingUnifiedSupplierIssues entity) {
        AccountingUnifiedSupplierIssues originEntity = accountingUnifiedSupplierIssuesMapper.selectByPrimaryKey(entity.getId());
//		AssertUtil.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        accountingUnifiedSupplierIssuesMapper.updateByPrimaryKey(originEntity);
    }

    public void deleteAccountingUnifiedSupplierIssues(Long id) {
        accountingUnifiedSupplierIssuesMapper.deleteByPrimaryKey(id);
    }

    public AccountingUnifiedSupplierIssues findAccountingUnifiedSupplierIssuesById(Long id) {
        AccountingUnifiedSupplierIssues entity = accountingUnifiedSupplierIssuesMapper.selectByPrimaryKey(id);
//		AssertUtil.notNull(entity);
        return entity;
    }

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

        // 样例 保留
//		AccountingUnifiedSupplierIssues accountingUnifiedSupplierIssuesConditions = BeanUtils.mapToBean(conditions,AccountingUnifiedSupplierIssues.class);

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

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

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

    }

    public void saveOrUpdateCheckByAccountingOrderIdAndAccountingOrderSn(AccountingUnifiedSupplierIssues entity) {

        AccountingUnifiedSupplierIssuesExample example = new AccountingUnifiedSupplierIssuesExample();
        example.clear();
        example.createCriteria()
                .andAccountingOrderIdEqualTo(entity.getAccountingOrderId())
                .andAccountingOrderSnEqualTo(entity.getAccountingOrderSn());

        List<AccountingUnifiedSupplierIssues> result = accountingUnifiedSupplierIssuesMapper.selectByExample(example);

        if (AnCollectionUtils.isEmpty(result)) {

            entity.setIsProblem(1);
            entity.setIsProcess(0);
            entity.setIsResult(0);
            entity.setIssuesComeFrom(1);

            entity.setCreateTime(DateUtil.date());
            entity.setCreateUserId(1L);
            entity.setUpdateTime(DateUtil.date());
            entity.setUpdateUserId(1L);

            entity.setProblemReport("");
            entity.setProcessReport("");
            entity.setResultReport("");

            accountingUnifiedSupplierIssuesMapper.insertSelective(entity);
        }

    }

    public void init001AccountingUnifiedSupplierIssuess(Integer isCreateExcel, LoginSysUser loginSysUser) {

        List<AccountingUnifiedSupplierIssues> result = accountingUnifiedSupplierIssuesMapperEx.init001AccountingUnifiedSupplierIssuess();

        result.forEach(entity -> {
            saveOrUpdateCheckByAccountingOrderIdAndAccountingOrderSn(entity);

            if (isCreateExcel != null && isCreateExcel.compareTo(1) == 0) {
                accountingUnifiedSupplierExportExcelService.exportAccountingUnifiedSupplierIssues(entity, loginSysUser);
            }
        });
    }
}