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

import cn.superdesk.uniorder.common.exception.utils.AssertUtils;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
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.AccountingAppeal;
import cn.superdesk.uniorder.dao.entity.accounting.AccountingAppealExample;
import cn.superdesk.uniorder.dao.mapper.accounting.AccountingAppealMapper;
import cn.superdesk.uniorder.dao.mapper.accounting.AccountingAppealMapperEx;
import cn.superdesk.uniorder.service.accounting.AccountingAppealService;
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: 2020-10-23 17:19:13
 * @UpdateUser: linjinyu
 * @UpdateDate: 2020-10-23 17:19:13
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class AccountingAppealServiceImpl implements AccountingAppealService {

    @Autowired
    private AccountingAppealMapper   accountingAppealMapper;
    @Autowired
    private AccountingAppealMapperEx accountingAppealMapperEx;


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

    @Override
    public int insertSelective(AccountingAppeal entity) {
        return accountingAppealMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(AccountingAppeal entity) {
        return accountingAppealMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public AccountingAppeal findById(Long id) {
        return accountingAppealMapper.selectByPrimaryKey(id);
    }

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

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

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


    public void addAccountingAppeal(AccountingAppeal entity) {

        log.info(AnJsonUtils.toPrettyJson(entity));

        accountingAppealMapperEx.insertSelective(entity);
    }

    public void updateAccountingAppeal(AccountingAppeal entity) {
        AccountingAppeal originEntity = accountingAppealMapper.selectByPrimaryKey(entity.getId());
        AssertUtils.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        accountingAppealMapper.updateByPrimaryKey(originEntity);
    }

    public void deleteAccountingAppeal(Long id) {
        accountingAppealMapper.deleteByPrimaryKey(id);
    }

    public AccountingAppeal findAccountingAppealById(Long id) {


        AccountingAppeal entity = accountingAppealMapper.selectByPrimaryKey(id);
        AssertUtils.notNull(entity);
        return entity;
    }

    public AccountingAppeal findAccountingAppealByAccountingOrderId(Long accountingOrderId) {
        AccountingAppealExample example = new AccountingAppealExample();
        example.createCriteria()
                .andAccountingOrderIdEqualTo(accountingOrderId);

        List<AccountingAppeal> result = accountingAppealMapper.selectByExample(example);

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

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

        // 样例 保留
//		AccountingAppeal accountingAppealConditions = BeanUtils.mapToBean(conditions,AccountingAppeal.class);

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

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

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

    }

}