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

import cn.hutool.core.collection.CollUtil;
import cn.superdesk.uniorder.common.enums.accounting.AccountingOrderDimCreateByEnum;
import cn.superdesk.uniorder.common.exception.utils.AssertUtils;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.accounting.unified.supplier.AccountingUnifiedSupplier;
import cn.superdesk.uniorder.dao.entity.accounting.unified.supplier.AccountingUnifiedSupplierOrder;
import cn.superdesk.uniorder.dao.entity.accounting.unified.supplier.AccountingUnifiedSupplierXGoods;
import cn.superdesk.uniorder.dao.entity.accounting.unified.supplier.AccountingUnifiedSupplierXGoodsExample;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrderGoods;
import cn.superdesk.uniorder.dao.mapper.accounting.unified.supplier.AccountingUnifiedSupplierXGoodsMapper;
import cn.superdesk.uniorder.dao.mapper.accounting.unified.supplier.AccountingUnifiedSupplierXGoodsMapperEx;
import cn.superdesk.uniorder.service.accounting.unified.supplier.AccountingUnifiedSupplierXGoodsService;
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.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2020-10-23 17:19:13
 * @UpdateUser: linjinyu
 * @UpdateDate: 2020-10-23 17:19:13
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Service
public class AccountingUnifiedSupplierXGoodsServiceImpl implements AccountingUnifiedSupplierXGoodsService {

    @Autowired
    private AccountingUnifiedSupplierXGoodsMapper   accountingUnifiedSupplierXGoodsMapper;
    @Autowired
    private AccountingUnifiedSupplierXGoodsMapperEx accountingUnifiedSupplierXGoodsMapperEx;


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


    @Override
    public int insertSelective(AccountingUnifiedSupplierXGoods entity) {
        return accountingUnifiedSupplierXGoodsMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(AccountingUnifiedSupplierXGoods entity) {
        return accountingUnifiedSupplierXGoodsMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public AccountingUnifiedSupplierXGoods findById(Long id) {
        return accountingUnifiedSupplierXGoodsMapper.selectByPrimaryKey(id);
    }

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

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

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


    public void addAccountingUnifiedSupplierXGoods(AccountingUnifiedSupplierXGoods entity) {
        accountingUnifiedSupplierXGoodsMapper.insertSelective(entity);
    }

    public void updateAccountingUnifiedSupplierXGoods(AccountingUnifiedSupplierXGoods entity) {
        AccountingUnifiedSupplierXGoods originEntity = accountingUnifiedSupplierXGoodsMapper.selectByPrimaryKey(entity.getId());
        AssertUtils.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        accountingUnifiedSupplierXGoodsMapper.updateByPrimaryKey(originEntity);
    }

    public void deleteAccountingUnifiedSupplierXGoods(Long id) {
        accountingUnifiedSupplierXGoodsMapper.deleteByPrimaryKey(id);
    }

    public AccountingUnifiedSupplierXGoods findAccountingUnifiedSupplierXGoodsById(Long id) {
        AccountingUnifiedSupplierXGoods entity = accountingUnifiedSupplierXGoodsMapper.selectByPrimaryKey(id);
        AssertUtils.notNull(entity);
        return entity;
    }

    public List<AccountingUnifiedSupplierXGoods> findByAccountingOrderSn(String accountingOrderSn) {

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

        criteria.andAccountingOrderSnEqualTo(accountingOrderSn);

        return accountingUnifiedSupplierXGoodsMapper.selectByExample(example);

    }

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

        // 样例 保留
//		AccountingUnifiedSupplierXGoods accountingUnifiedSupplierXGoodsConditions = BeanUtils.mapToBean(conditions,AccountingUnifiedSupplierXGoods.class);

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

        PageHelper.startPage(pageNun, pageSize);
        List<AccountingUnifiedSupplierXGoods>     resultList = accountingUnifiedSupplierXGoodsMapper.selectByExample(example);
        PageInfo<AccountingUnifiedSupplierXGoods> pageInfo   = new PageInfo<AccountingUnifiedSupplierXGoods>(resultList);
        PageHelper.clearPage();

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

    }

    /****************************************** 工具方法 start ******************************************/

    /**
     * 组成 合成 而已 返回 xGoods !!没在用!!
     *
     * @param accountingUnifiedSupplier
     * @param accountingUnifiedSupplierOrder
     * @param unifiedOrderGoods
     * @param accountingOrderDimCreateByEnum
     * @param processDate
     * @return
     */
    public AccountingUnifiedSupplierXGoods composeAccountingUnifiedSupplierXGoods(AccountingUnifiedSupplier accountingUnifiedSupplier,
            /* 注意 */AccountingUnifiedSupplierOrder accountingUnifiedSupplierOrder,
                                                                                  UnifiedOrderGoods unifiedOrderGoods,
                                                                                  AccountingOrderDimCreateByEnum accountingOrderDimCreateByEnum,
                                                                                  Date processDate) {

        AccountingUnifiedSupplierXGoods xGoods = new AccountingUnifiedSupplierXGoods();

        xGoods.setDimCreateBy(accountingOrderDimCreateByEnum.getKey());

        xGoods.setAccountingOrderId(accountingUnifiedSupplier.getId());
        xGoods.setAccountingOrderSn(accountingUnifiedSupplier.getAccountingOrderSn());

        xGoods.setUnifiedOrderId(accountingUnifiedSupplierOrder.getUnifiedOrderId());
        xGoods.setOrderSn(accountingUnifiedSupplierOrder.getOrderSn());

        xGoods.setUnifiedOrderGoodsId(unifiedOrderGoods.getId());
        xGoods.setGoodsId(unifiedOrderGoods.getGoodsId());
        xGoods.setOutSnapshotOrderGoodsId(unifiedOrderGoods.getOutSnapshotOrderGoodsId());

        xGoods.setCreateUserId(1L);
        xGoods.setCreateTime(processDate);
        xGoods.setUpdateUserId(1L);
        xGoods.setCreateTime(processDate);

        return xGoods;
    }

    @Override
    public void deleteByAccountingOrderSn(String accountingOrderSn) {
        AccountingUnifiedSupplierXGoodsExample          example  = new AccountingUnifiedSupplierXGoodsExample();
        AccountingUnifiedSupplierXGoodsExample.Criteria criteria = example.createCriteria();

        criteria.andAccountingOrderSnEqualTo(accountingOrderSn);

        accountingUnifiedSupplierXGoodsMapper.deleteByExample(example);
    }


    /**
     * 重构 再再 mark by linjinyu at 20210202 修改为 批量插入
     * 与上边组合使用
     *
     * @param processList
     * @return
     */
    public int insertBatch(List<AccountingUnifiedSupplierXGoods> processList) {

        if (CollUtil.isEmpty(processList)) {
            return 0;
        }

        return accountingUnifiedSupplierXGoodsMapperEx.insertBatch(processList);

    }
    /****************************************** 工具方法   end ******************************************/

}