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

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.AccountingUnifiedCustomerOrder;
import cn.superdesk.uniorder.dao.entity.accounting.unified.customer.AccountingUnifiedCustomerOrderExample;
import cn.superdesk.uniorder.dao.mapper.accounting.unified.customer.AccountingUnifiedCustomerOrderMapper;
import cn.superdesk.uniorder.service.accounting.unified.customer.AccountingUnifiedCustomerOrderService;
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-01-06 17:46:38
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-01-06 17:46:38
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class AccountingUnifiedCustomerOrderServiceImpl implements AccountingUnifiedCustomerOrderService {

    @Autowired
    private AccountingUnifiedCustomerOrderMapper accountingUnifiedCustomerOrderMapper;


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

    @Override
    public int insertSelective(AccountingUnifiedCustomerOrder entity) {
        return accountingUnifiedCustomerOrderMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(AccountingUnifiedCustomerOrder entity) {
        return accountingUnifiedCustomerOrderMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

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

        AccountingUnifiedCustomerOrderExample deleteByExample = new AccountingUnifiedCustomerOrderExample();
        deleteByExample.createCriteria()
                .andDzIdEqualTo(accountingOrderId)
                .andOrderSnEqualTo(orderSn);

        accountingUnifiedCustomerOrderMapper.deleteByExample(deleteByExample);
    }

    @Override
    public AccountingUnifiedCustomerOrder findById(Long id) {
        return accountingUnifiedCustomerOrderMapper.selectByPrimaryKey(id);
    }

    public AccountingUnifiedCustomerOrder findByByAccountingOrderIdAndOrderSn(Long accountingOrderId, String orderSn) {

        AccountingUnifiedCustomerOrderExample selectByExample = new AccountingUnifiedCustomerOrderExample();
        selectByExample.createCriteria()
                .andDzIdEqualTo(accountingOrderId)
                .andOrderSnEqualTo(orderSn);

        List<AccountingUnifiedCustomerOrder> result = accountingUnifiedCustomerOrderMapper.selectByExample(selectByExample);

        if (AnCollectionUtils.isEmpty(result)) {
            return null;
        } else {
            return result.get(0);
        }
    }

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

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

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


    public void addAccountingUnifiedCustomerOrder(AccountingUnifiedCustomerOrder entity) {
        accountingUnifiedCustomerOrderMapper.insertSelective(entity);
    }

    public void updateAccountingUnifiedCustomerOrder(AccountingUnifiedCustomerOrder entity) {
        AccountingUnifiedCustomerOrder originEntity = accountingUnifiedCustomerOrderMapper.selectByPrimaryKey(entity.getId());
        AssertUtils.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        accountingUnifiedCustomerOrderMapper.updateByPrimaryKey(originEntity);
    }

    public void deleteAccountingUnifiedCustomerOrder(Long id) {
        accountingUnifiedCustomerOrderMapper.deleteByPrimaryKey(id);
    }

    public AccountingUnifiedCustomerOrder findAccountingUnifiedCustomerOrderById(Long id) {
        AccountingUnifiedCustomerOrder entity = accountingUnifiedCustomerOrderMapper.selectByPrimaryKey(id);
        AssertUtils.notNull(entity);
        return entity;
    }

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

        // 样例 保留
//		AccountingUnifiedCustomerOrder accountingUnifiedCustomerOrderConditions = BeanUtils.mapToBean(conditions,AccountingUnifiedCustomerOrder.class);

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

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

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

    }

}