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

import cn.superdesk.uniorder.common.enums.common.CommonProcessResultEnum;
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.operation.UnifiedOrderIssuesFlowing;
import cn.superdesk.uniorder.dao.entity.operation.UnifiedOrderIssuesFlowingExample;
import cn.superdesk.uniorder.dao.mapper.operation.UnifiedOrderIssuesFlowingMapper;
import cn.superdesk.uniorder.dto.param.operation.UnifiedOrderIssuesFlowingQueryCriteria;
import cn.superdesk.uniorder.dto.vo.operation.UnifiedOrderIssuesFlowingVo;
import cn.superdesk.uniorder.service.operation.UnifiedOrderIssuesFlowingService;
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.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 问题订单-流水记录
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2021-06-25 14:22:13
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-06-25 14:22:13
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Service
public class UnifiedOrderIssuesFlowingServiceImpl implements UnifiedOrderIssuesFlowingService {

    @Autowired
    private UnifiedOrderIssuesFlowingMapper unifiedOrderIssuesFlowingMapper;


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


    @Override
    public int insertSelective(UnifiedOrderIssuesFlowing entity) {
        return unifiedOrderIssuesFlowingMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(UnifiedOrderIssuesFlowing entity) {
        return unifiedOrderIssuesFlowingMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public UnifiedOrderIssuesFlowing findById(Long id) {
        return unifiedOrderIssuesFlowingMapper.selectByPrimaryKey(id);
    }

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

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

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


    public void addUnifiedOrderIssuesFlowing(UnifiedOrderIssuesFlowing entity) {
        unifiedOrderIssuesFlowingMapper.insertSelective(entity);
    }

    public void updateUnifiedOrderIssuesFlowing(UnifiedOrderIssuesFlowing entity) {
        UnifiedOrderIssuesFlowing originEntity = unifiedOrderIssuesFlowingMapper.selectByPrimaryKey(entity.getId());
//		AssertUtil.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        unifiedOrderIssuesFlowingMapper.updateByPrimaryKeyWithBLOBs(originEntity);
    }

    public void deleteUnifiedOrderIssuesFlowing(Long id) {
        unifiedOrderIssuesFlowingMapper.deleteByPrimaryKey(id);
    }

    public UnifiedOrderIssuesFlowing findUnifiedOrderIssuesFlowingById(Long id) {
        UnifiedOrderIssuesFlowing entity = unifiedOrderIssuesFlowingMapper.selectByPrimaryKey(id);
//		AssertUtil.notNull(entity);
        return entity;
    }

    public UnifiedOrderIssuesFlowing findUnifiedOrderIssuesFlowingByUid(String uid) {

        UnifiedOrderIssuesFlowingExample example = new UnifiedOrderIssuesFlowingExample();
        example.createCriteria()
                .andUidEqualTo(uid);

        List<UnifiedOrderIssuesFlowing> resultList = unifiedOrderIssuesFlowingMapper.selectByExampleWithBLOBs(example);

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

        return null;
    }

    public List<UnifiedOrderIssuesFlowing> findUnifiedOrderIssuesFlowingByOrderSn(String orderSn) {
        UnifiedOrderIssuesFlowingExample example = new UnifiedOrderIssuesFlowingExample();
        example.createCriteria()
                .andOrderSnEqualTo(orderSn);

        return unifiedOrderIssuesFlowingMapper.selectByExampleWithBLOBs(example);
    }

    public R<List<UnifiedOrderIssuesFlowingVo>> pageQuery(UnifiedOrderIssuesFlowingQueryCriteria queryCriteria, int pageNun, int pageSize) {

        // 样例 保留
//		UnifiedOrderIssuesFlowing unifiedOrderIssuesFlowingConditions = BeanUtils.mapToBean(conditions,UnifiedOrderIssuesFlowing.class);

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

        if (null != queryCriteria.getId()) {

            criteria.andIdEqualTo(queryCriteria.getId());
        }

        if (null != queryCriteria.getUid()) {

            criteria.andUidEqualTo(queryCriteria.getUid());
        }

        if (null != queryCriteria.getOrderSn()) {

            criteria.andOrderSnEqualTo(queryCriteria.getOrderSn());
        }

        example.setOrderByClause("create_time DESC");

        PageHelper.startPage(pageNun, pageSize);

        List<UnifiedOrderIssuesFlowing> resultList = unifiedOrderIssuesFlowingMapper.selectByExampleWithBLOBs(example);

        // 附加信息
        List<UnifiedOrderIssuesFlowingVo> resultListAdditiona = additionalInfoPageQuery(resultList);


        PageInfo<UnifiedOrderIssuesFlowingVo> page = new PageInfo<UnifiedOrderIssuesFlowingVo>(resultListAdditiona);
        PageHelper.clearPage();

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

    }

    /**
     * 附加信息
     *
     * @param resultList
     * @return
     */
    private List<UnifiedOrderIssuesFlowingVo> additionalInfoPageQuery(List<UnifiedOrderIssuesFlowing> resultList) {

        List<UnifiedOrderIssuesFlowingVo> resultListAdditiona = new ArrayList<>();

        resultList.forEach(item -> {
            UnifiedOrderIssuesFlowingVo vo = AnBeanUtils.copy(item, UnifiedOrderIssuesFlowingVo.class);
            vo.setProcessResultStr(CommonProcessResultEnum.get(vo.getProcessResult()).getDesc());

            resultListAdditiona.add(vo);
        });

        return resultListAdditiona;

    }


}