package cn.superdesk.uniorder.service.accounting.unified.customer.impl;

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.numerical.AnCollectionUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.accounting.unified.customer.AccountingUnifiedCustomer;
import cn.superdesk.uniorder.dao.entity.accounting.unified.customer.AccountingUnifiedCustomerOrder;
import cn.superdesk.uniorder.dao.entity.accounting.unified.customer.AccountingUnifiedCustomerXGoods;
import cn.superdesk.uniorder.dao.entity.accounting.unified.customer.AccountingUnifiedCustomerXGoodsExample;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrder;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrderGoods;
import cn.superdesk.uniorder.dao.mapper.accounting.unified.customer.AccountingUnifiedCustomerXGoodsMapper;
import cn.superdesk.uniorder.dao.mapper.accounting.unified.customer.AccountingUnifiedCustomerXGoodsMapperEx;
import cn.superdesk.uniorder.service.accounting.unified.customer.AccountingUnifiedCustomerXGoodsService;
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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2021-01-06 17:46:38
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-01-06 17:46:38
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class AccountingUnifiedCustomerXGoodsServiceImpl implements AccountingUnifiedCustomerXGoodsService {

    @Autowired
    private AccountingUnifiedCustomerXGoodsMapper   accountingUnifiedCustomerXGoodsMapper;
    @Autowired
    private AccountingUnifiedCustomerXGoodsMapperEx accountingUnifiedCustomerXGoodsMapperEx;


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

    @Override
    public int insertSelective(AccountingUnifiedCustomerXGoods entity) {
        return accountingUnifiedCustomerXGoodsMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(AccountingUnifiedCustomerXGoods entity) {
        return accountingUnifiedCustomerXGoodsMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public void deleteByAccountingOrderIdAndOrderSn(Long accountingOrderId, String orderSn) {

        AccountingUnifiedCustomerXGoodsExample deleteByExample = new AccountingUnifiedCustomerXGoodsExample();
        deleteByExample.createCriteria()
                .andAccountingOrderIdEqualTo(accountingOrderId)
                .andOrderSnEqualTo(orderSn);
        accountingUnifiedCustomerXGoodsMapper.deleteByExample(deleteByExample);
    }

    @Override
    public void deleteByAccountingOrderIdAndOrderSnAndSysUnifiedOrderGoodsIds(Long accountingOrderId, String orderSn, List<Long> sysUnifiedOrderGoodsIds) {

        AccountingUnifiedCustomerXGoodsExample deleteByExample = new AccountingUnifiedCustomerXGoodsExample();
        deleteByExample.createCriteria()
                .andAccountingOrderIdEqualTo(accountingOrderId)
                .andOrderSnEqualTo(orderSn)
                .andUnifiedOrderGoodsIdIn(sysUnifiedOrderGoodsIds);
        accountingUnifiedCustomerXGoodsMapper.deleteByExample(deleteByExample);

    }

    @Override
    public AccountingUnifiedCustomerXGoods findById(Long id) {
        return accountingUnifiedCustomerXGoodsMapper.selectByPrimaryKey(id);
    }

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

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

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


    public void addAccountingUnifiedCustomerXGoods(AccountingUnifiedCustomerXGoods entity) {
        accountingUnifiedCustomerXGoodsMapper.insertSelective(entity);
    }

    public void updateAccountingUnifiedCustomerXGoods(AccountingUnifiedCustomerXGoods entity) {
        AccountingUnifiedCustomerXGoods originEntity = accountingUnifiedCustomerXGoodsMapper.selectByPrimaryKey(entity.getId());
        AssertUtils.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        accountingUnifiedCustomerXGoodsMapper.updateByPrimaryKey(originEntity);
    }

    public void deleteAccountingUnifiedCustomerXGoods(Long id) {
        accountingUnifiedCustomerXGoodsMapper.deleteByPrimaryKey(id);
    }

    public AccountingUnifiedCustomerXGoods findAccountingUnifiedCustomerXGoodsById(Long id) {
        AccountingUnifiedCustomerXGoods entity = accountingUnifiedCustomerXGoodsMapper.selectByPrimaryKey(id);
        AssertUtils.notNull(entity);
        return entity;
    }

    public List<AccountingUnifiedCustomerXGoods> findByAccountingOrderId(Long accountingOrderId) {

        List<AccountingUnifiedCustomerXGoods> result = new ArrayList<>();

        AccountingUnifiedCustomerXGoodsExample example = new AccountingUnifiedCustomerXGoodsExample();
        example.createCriteria()
                .andAccountingOrderIdEqualTo(accountingOrderId);

        result = accountingUnifiedCustomerXGoodsMapper.selectByExample(example);

        return result;

    }

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

        // 样例 保留
//		AccountingUnifiedCustomerXGoods accountingUnifiedCustomerXGoodsConditions = BeanUtils.mapToBean(conditions,AccountingUnifiedCustomerXGoods.class);

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

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

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

    }


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

    public AccountingUnifiedCustomerXGoods composeAccountingUnifiedCustomerXGoods01(AccountingUnifiedCustomer accountingUnifiedCustomer,
            /* 注意 */UnifiedOrder unifiedOrder,
                                                                                    UnifiedOrderGoods unifiedOrderGoods,
                                                                                    AccountingOrderDimCreateByEnum accountingOrderDimCreateByEnum,
                                                                                    Date processDate) {

        AccountingUnifiedCustomerXGoods xGoods = new AccountingUnifiedCustomerXGoods();

        xGoods.setDimCreateBy(accountingOrderDimCreateByEnum.getKey());

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

        xGoods.setUnifiedOrderId(unifiedOrder.getId());
        xGoods.setOrderSn(unifiedOrder.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;
    }

    /**
     * 组成 合成 而已 返回 xGoods
     *
     * @param accountingUnifiedCustomer
     * @param accountingUnifiedCustomerOrder
     * @param unifiedOrderGoods
     * @param accountingOrderDimCreateByEnum
     * @param processDate
     * @return
     */
    public AccountingUnifiedCustomerXGoods composeAccountingUnifiedCustomerXGoods02(AccountingUnifiedCustomer accountingUnifiedCustomer,
            /* 注意 */AccountingUnifiedCustomerOrder accountingUnifiedCustomerOrder,
                                                                                    UnifiedOrderGoods unifiedOrderGoods,
                                                                                    AccountingOrderDimCreateByEnum accountingOrderDimCreateByEnum,
                                                                                    Date processDate) {

        AccountingUnifiedCustomerXGoods xGoods = new AccountingUnifiedCustomerXGoods();

        xGoods.setDimCreateBy(accountingOrderDimCreateByEnum.getKey());

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

        xGoods.setUnifiedOrderId(accountingUnifiedCustomerOrder.getOrderId());
        xGoods.setOrderSn(accountingUnifiedCustomerOrder.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;
    }


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

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

        return accountingUnifiedCustomerXGoodsMapperEx.insertBatch(processList);

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

}