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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.superdesk.uniorder.common.enums.common.CommonProcessResultEnum;
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.base.UnifiedOrder;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrderGoods;
import cn.superdesk.uniorder.dao.entity.operation.UnifiedOrderIssues;
import cn.superdesk.uniorder.dao.entity.operation.UnifiedOrderIssuesExample;
import cn.superdesk.uniorder.dao.entity.operation.UnifiedOrderIssuesFlowing;
import cn.superdesk.uniorder.dao.mapper.operation.UnifiedOrderIssuesMapper;
import cn.superdesk.uniorder.dao.mapper.operation.UnifiedOrderIssuesMapperEx;
import cn.superdesk.uniorder.dto.param.operation.UnifiedOrderIssuesQueryCriteria;
import cn.superdesk.uniorder.service.operation.UnifiedOrderIssuesFlowingService;
import cn.superdesk.uniorder.service.operation.UnifiedOrderIssuesService;
import cn.superdesk.uniorder.service.order.base.CommonOrderProcessService;
import cn.superdesk.uniorder.service.order.base.UnifiedOrderGoodsService;
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-29 16:47:14
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-01-29 16:47:14
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class UnifiedOrderIssuesServiceImpl implements UnifiedOrderIssuesService {

    @Autowired
    private UnifiedOrderIssuesMapper   unifiedOrderIssuesMapper;
    @Autowired
    private UnifiedOrderIssuesMapperEx unifiedOrderIssuesMapperEx;
    @Autowired
    private CommonOrderProcessService  commonOrderProcessService;

    @Autowired
    private UnifiedOrderGoodsService unifiedOrderGoodsService;

    @Autowired
    private UnifiedOrderIssuesFlowingService unifiedOrderIssuesFlowingService;


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


    @Override
    public int insertSelective(UnifiedOrderIssues entity) {
        return unifiedOrderIssuesMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(UnifiedOrderIssues entity) {
        return unifiedOrderIssuesMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public UnifiedOrderIssues findById(Long id) {
        return unifiedOrderIssuesMapper.selectByPrimaryKey(id);
    }

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

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

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


    public void addUnifiedOrderIssues(UnifiedOrderIssues entity) {
        unifiedOrderIssuesMapper.insertSelective(entity);
    }

    public void updateUnifiedOrderIssues(UnifiedOrderIssues entity) {
        UnifiedOrderIssues originEntity = unifiedOrderIssuesMapper.selectByPrimaryKey(entity.getId());
        AssertUtils.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        unifiedOrderIssuesMapper.updateByPrimaryKey(originEntity);
    }

    public void deleteUnifiedOrderIssues(Long id) {
        unifiedOrderIssuesMapper.deleteByPrimaryKey(id);
    }

    public UnifiedOrderIssues findUnifiedOrderIssuesById(Long id) {
        UnifiedOrderIssues entity = unifiedOrderIssuesMapper.selectByPrimaryKey(id);
        AssertUtils.notNull(entity);
        return entity;
    }

    public UnifiedOrderIssues findUnifiedOrderIssuesByOrderSn(String orderSn) {

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

        criteria
                .andOrderSnEqualTo(orderSn);


        List<UnifiedOrderIssues> resultList = unifiedOrderIssuesMapper.selectByExample(example);

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

        return null;

    }

    public R<List<UnifiedOrderIssues>> pageQuery(UnifiedOrderIssuesQueryCriteria queryCriteria, int pageNun, int pageSize) {

        // 样例 保留
//		UnifiedOrderIssues unifiedOrderIssuesConditions = BeanUtils.mapToBean(conditions,UnifiedOrderIssues.class);

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

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

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

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

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

    }


    public List<UnifiedOrder> getToDoUnifiedOrderIssuesList(int pageNun, int pageSize) {

//		int pageNun  = 1;
//		int pageSize = 10;

        PageHelper.startPage(pageNun, pageSize);

        List<UnifiedOrder> resultList = unifiedOrderIssuesMapperEx.getToDoUnifiedOrderIssuesList();

        PageInfo<UnifiedOrder> page = new PageInfo<UnifiedOrder>(resultList);
        PageHelper.clearPage();

        return resultList;
    }

    public void runTaskUnifiedOrderIssues(UnifiedOrder unifiedOrder) {

        List<UnifiedOrderGoods> unifiedOrderGoodsList = unifiedOrderGoodsService.findByOrderSn(unifiedOrder.getOrderSn());

        commonOrderProcessService.processOrderAmount(unifiedOrder, unifiedOrderGoodsList, "");

//		UnifiedOrderIssues unifiedOrderIssues =
//
//		unifiedOrderIssuesMapper.insert(unifiedOrderIssues);
    }


    /******************************************  start ******************************************/

    public void developCall(
            String sourceJson,
            String processJson,
            String orderSn,
            String uid) {

        touch(orderSn);
        flowingInit(sourceJson, processJson, orderSn, uid);
    }

    public void developCallBack(Integer processResult, String processReport, String uid) {

        UnifiedOrderIssuesFlowing unifiedOrderIssuesFlowing = unifiedOrderIssuesFlowingService.findUnifiedOrderIssuesFlowingByUid(uid);

        if (null != unifiedOrderIssuesFlowing) {

            UnifiedOrderIssuesFlowing unifiedOrderIssuesFlowing4CallBackUpdate = new UnifiedOrderIssuesFlowing();
            unifiedOrderIssuesFlowing4CallBackUpdate.setId(unifiedOrderIssuesFlowing.getId());
            unifiedOrderIssuesFlowing4CallBackUpdate.setProcessResult(processResult);
            unifiedOrderIssuesFlowing4CallBackUpdate.setProcessReport(processReport);

//			unifiedOrderIssuesFlowing4CallBackUpdate.setProcessResult(1);

            unifiedOrderIssuesFlowingService.updateUnifiedOrderIssuesFlowing(unifiedOrderIssuesFlowing4CallBackUpdate);

            /****************************************** 更新 start ******************************************/

            UnifiedOrderIssues unifiedOrderIssues = findUnifiedOrderIssuesByOrderSn(unifiedOrderIssuesFlowing.getOrderSn());

            if (null != unifiedOrderIssues) {

                UnifiedOrderIssues unifiedOrderIssues4TouchUpdate = new UnifiedOrderIssues();

                unifiedOrderIssues4TouchUpdate.setId(unifiedOrderIssues.getId());
                unifiedOrderIssues4TouchUpdate.setIsResult(processResult);
                unifiedOrderIssues4TouchUpdate.setUpdateTime(DateUtil.date());

                updateUnifiedOrderIssues(unifiedOrderIssues4TouchUpdate);
            }
        } else {
            log.error("{}", StrUtil.format("订单问题记录回调出问题:回调更新体为空?^_^{}^_^{}^_^{}", processResult, processReport, uid));
        }


    }

    public void touch(String orderSn) {

        UnifiedOrderIssues unifiedOrderIssues = findUnifiedOrderIssuesByOrderSn(orderSn);

        if (null == unifiedOrderIssues) {

            unifiedOrderIssues = new UnifiedOrderIssues();
            unifiedOrderIssues.setIssuesComeFrom(1);
            unifiedOrderIssues.setUnifiedOrderId(0L);
            unifiedOrderIssues.setOrderSn(orderSn);
            unifiedOrderIssues.setIsProblem(0);
            unifiedOrderIssues.setIsProcess(0);
            unifiedOrderIssues.setIsResult(CommonProcessResultEnum.初始.getKey());
            unifiedOrderIssues.setProblemReport("");
            unifiedOrderIssues.setProcessReport("");
            unifiedOrderIssues.setResultReport("");

            unifiedOrderIssues.setCreateTime(DateUtil.date());


            addUnifiedOrderIssues(unifiedOrderIssues);

        } else { // 这步骤有点多余,因为违背此方法的用法

            UnifiedOrderIssues unifiedOrderIssues4TouchUpdate = new UnifiedOrderIssues();
            unifiedOrderIssues4TouchUpdate.setId(unifiedOrderIssues.getId());
            unifiedOrderIssues4TouchUpdate.setUpdateTime(DateUtil.date());

            updateUnifiedOrderIssues(unifiedOrderIssues4TouchUpdate);
        }
    }

    public void flowingInit(String sourceJson, String processJson, String orderSn, String uid) {

        UnifiedOrderIssuesFlowing unifiedOrderIssuesFlowing = new UnifiedOrderIssuesFlowing();

        unifiedOrderIssuesFlowing.setUid(uid);
        unifiedOrderIssuesFlowing.setOrderSn(orderSn);
        unifiedOrderIssuesFlowing.setSourceJson(sourceJson);
        unifiedOrderIssuesFlowing.setProcessJson(processJson);

        unifiedOrderIssuesFlowing.setProcessResult(CommonProcessResultEnum.初始.getKey());

        unifiedOrderIssuesFlowing.setCreateTime(DateUtil.date());

        unifiedOrderIssuesFlowingService.addUnifiedOrderIssuesFlowing(unifiedOrderIssuesFlowing);
    }

}