package com.liquidnet.service.platform.service.express.shunfeng;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.commons.lang.util.*;
import com.liquidnet.service.kylin.constant.KylinRedisConst;
import com.liquidnet.service.kylin.dto.param.*;
import com.liquidnet.service.kylin.dto.vo.express.KylinOrderExpressRouteVo;
import com.liquidnet.service.kylin.dto.vo.express.KylinOrderExpressVo;
import com.liquidnet.service.kylin.dto.vo.mongo.KylinRoadShowVo;
import com.liquidnet.service.kylin.entity.*;
import com.liquidnet.service.kylin.mapper.*;
import com.liquidnet.service.kylin.service.IKylinOrderExpress;
import com.liquidnet.service.platform.utils.DataUtils;
import com.liquidnet.service.platform.utils.ShunfengSignUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 演出 服务实现类
 * </p>
 *
 * @author liquidnet
 * @since 2021-05-05
 */
@Service
@Slf4j
public class PerformancesExpressCallbackServiceImpl extends ServiceImpl<KylinOrderExpressMapper, KylinOrderExpress> implements IKylinOrderExpress {

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    private ShunfengSignUtils shunfengSignUtils;

    @Autowired
    private KylinOrderExpressMapper kylinOrderExpressMapper;

    @Autowired
    private KylinOrderExpressFeeInfoMapper kylinOrderExpressFeeInfoMapper;

    @Autowired
    private KylinOrderExpressRouteMapper kylinOrderExpressRouteMapper;

    @Autowired
    private KylinOrderExpressStatusMapper kylinOrderExpressStatusMapper;

    @Autowired
    private KylinOrderExpressPerformancesMapper kylinOrderExpressPerformancesMapper;

    @Autowired
    private KylinOrderTicketRelationsMapper kylinOrderTicketRelationsMapper;

    @Autowired
    HttpServletRequest httpServletRequest;

    @Autowired
    private DataUtils dataUtils;

    public boolean orderStatus(@RequestBody String params) {
        boolean res = shunfengSignUtils.receiveRequestAndCheckSign(params, httpServletRequest);
        if (res) {
            PerformanceExpressPushStatusParam performanceExpressPushStatusParam = JsonUtils.fromJson(params, PerformanceExpressPushStatusParam.class);
            KylinOrderExpressStatus kylinOrderExpressStatus = new KylinOrderExpressStatus();
            String orderExpressStatusId = IDGenerator.nextSnowId();

            KylinOrderExpress orderExpressInfo = kylinOrderExpressMapper.selectOne(new QueryWrapper<KylinOrderExpress>().eq("mailno", performanceExpressPushStatusParam.getWaybillNo()));
            kylinOrderExpressStatus.setOrderExpressStatusId(orderExpressStatusId);
            kylinOrderExpressStatus.setOrderExpressId(orderExpressInfo.getOrderExpressId());
            kylinOrderExpressStatus.setOrderStateCode(performanceExpressPushStatusParam.getOrderStateCode());
            kylinOrderExpressStatus.setOrderStateDesc(performanceExpressPushStatusParam.getOrderStateDesc());
            kylinOrderExpressStatus.setEmpCode(performanceExpressPushStatusParam.getEmpCode());
            kylinOrderExpressStatus.setEmpPhone(performanceExpressPushStatusParam.getEmpPhone());
            kylinOrderExpressStatus.setNetCode(performanceExpressPushStatusParam.getNetCode());
            kylinOrderExpressStatus.setLastTime(performanceExpressPushStatusParam.getLastTime());
            kylinOrderExpressStatus.setBookTime(performanceExpressPushStatusParam.getBookTime());
            kylinOrderExpressStatus.setCarrierCode(performanceExpressPushStatusParam.getCarrierCode());
            kylinOrderExpressStatus.setCreatedAt(DateUtil.getNowTime());
            kylinOrderExpressStatusMapper.insert(kylinOrderExpressStatus);
            return true;
        }

        return false;
    }

    public boolean orderFreightList(@RequestBody String params) {
        boolean res = shunfengSignUtils.receiveRequestAndCheckSign(params, httpServletRequest);
        if (res) {
            PerformanceExpressPushFreightParam performanceExpressPushFreightParam = JsonUtils.fromJson(params, PerformanceExpressPushFreightParam.class);
            KylinOrderExpress orderExpressInfo = kylinOrderExpressMapper.selectOne(new QueryWrapper<KylinOrderExpress>().eq("mailno", performanceExpressPushFreightParam.getWaybillNo()));
            String orderExpressId = orderExpressInfo.getOrderExpressId();

            kylinOrderExpressFeeInfoMapper.delete(new QueryWrapper<KylinOrderExpressFeeInfo>().eq("order_express_id", orderExpressId));

            List<PerformanceExpressPushFreightInfoParam> feeList = performanceExpressPushFreightParam.getFeeList();
            for (PerformanceExpressPushFreightInfoParam feeInfo : feeList) {
                KylinOrderExpressFeeInfo kylinOrderExpressFeeInfo = new KylinOrderExpressFeeInfo();
                String orderExpressFeeInfoId = IDGenerator.nextSnowId();
                kylinOrderExpressFeeInfo.setOrderExpressFeeInfoId(orderExpressFeeInfoId);
                kylinOrderExpressFeeInfo.setOrderExpressId(orderExpressId);
                kylinOrderExpressFeeInfo.setType(feeInfo.getFeeIndType().toString());
                kylinOrderExpressFeeInfo.setName(feeInfo.getFeeTypeCode());
                kylinOrderExpressFeeInfo.setValue(BigDecimal.valueOf(feeInfo.getFeeAmt()));
                kylinOrderExpressFeeInfo.setCreatedAt(DateUtil.getNowTime());
                kylinOrderExpressFeeInfoMapper.insert(kylinOrderExpressFeeInfo);
            }
            return true;
        }

        return false;
    }

    public boolean orderRoute(@RequestBody String params) {
//        boolean res = shunfengSignUtils.receiveRequestAndCheckSign(params, httpServletRequest);
        if (true) {
            PerformanceExpressPushRouteParam performanceExpressPushRouteParam = JsonUtils.fromJson(params, PerformanceExpressPushRouteParam.class);
            KylinOrderExpressRoute kylinOrderExpressRoute = new KylinOrderExpressRoute();
            String orderExpressRouteId = IDGenerator.nextSnowId();

            KylinOrderExpress orderExpressInfo = kylinOrderExpressMapper.selectOne(new QueryWrapper<KylinOrderExpress>().eq("mailno", performanceExpressPushRouteParam.getMailno()));
            KylinOrderExpress kylinOrderExpress = new KylinOrderExpress();
            kylinOrderExpress.setExpressStatus(Integer.valueOf(performanceExpressPushRouteParam.getOpCode()));
            kylinOrderExpressMapper.update(kylinOrderExpress,
                    Wrappers.lambdaQuery(KylinOrderExpress.class).eq(KylinOrderExpress::getOrderExpressId, orderExpressInfo.getOrderExpressId()));

            kylinOrderExpressRoute.setOrderExpressRouteId(orderExpressRouteId);
            kylinOrderExpressRoute.setOrderExpressId(orderExpressInfo.getOrderExpressId());
            kylinOrderExpressRoute.setAcceptAddress(performanceExpressPushRouteParam.getAcceptAddress());
            kylinOrderExpressRoute.setAcceptTotaltime(performanceExpressPushRouteParam.getAcceptTime());
            kylinOrderExpressRoute.setRemark(performanceExpressPushRouteParam.getRemark());
            kylinOrderExpressRoute.setOpcode(performanceExpressPushRouteParam.getOpCode());
            kylinOrderExpressRoute.setCreatedAt(DateUtil.getNowTime());
            kylinOrderExpressRouteMapper.insert(kylinOrderExpressRoute);

            KylinOrderExpressVo kylinOrderExpressVo = new KylinOrderExpressVo();
            BeanUtils.copyProperties(orderExpressInfo, kylinOrderExpressVo);
            kylinOrderExpressVo.setExpressStatus(Integer.valueOf(performanceExpressPushRouteParam.getOpCode()));
            dataUtils.setOrderExpressInfo(orderExpressInfo.getOrderTicketsId(), kylinOrderExpressVo);

            KylinOrderExpressRouteVo kylinOrderExpressRouteVo = new KylinOrderExpressRouteVo();
            BeanUtils.copyProperties(kylinOrderExpressRoute, kylinOrderExpressRouteVo);
            kylinOrderExpressRouteVo.setOrderTicketsId(orderExpressInfo.getOrderTicketsId());
            mongoTemplate.insert(kylinOrderExpressRouteVo, KylinOrderExpressRouteVo.class.getSimpleName());

            dataUtils.delOrderExpressRoute(orderExpressInfo.getOrderTicketsId());

            // 记录演出数量
            if (50 == Integer.valueOf(performanceExpressPushRouteParam.getOpCode()) || 80 == Integer.valueOf(performanceExpressPushRouteParam.getOpCode())) {
                KylinOrderTicketRelations relationsInfo = kylinOrderTicketRelationsMapper.selectOne(new QueryWrapper<KylinOrderTicketRelations>().eq("order_id", orderExpressInfo.getOrderTicketsId()));
                KylinOrderExpressPerformances expressPerformances = kylinOrderExpressPerformancesMapper.selectOne(new QueryWrapper<KylinOrderExpressPerformances>().eq("performances_id", relationsInfo.getPerformanceId()));
                KylinOrderExpressPerformances kylinOrderExpressPerformances = new KylinOrderExpressPerformances();
                if (null != expressPerformances) {
                    if (50 == Integer.valueOf(performanceExpressPushRouteParam.getOpCode())) {
                        kylinOrderExpressPerformances.setTotal2(expressPerformances.getTotal2() + 1);
                    }
                    if (80 == Integer.valueOf(performanceExpressPushRouteParam.getOpCode())) {
                        kylinOrderExpressPerformances.setTotal3(expressPerformances.getTotal3() + 1);
                    }
                    kylinOrderExpressPerformances.setUpdatedAt(DateUtil.getNowTime());
                    kylinOrderExpressPerformancesMapper.update(kylinOrderExpressPerformances,
                            new QueryWrapper<KylinOrderExpressPerformances>().eq("performances_id", relationsInfo.getPerformanceId()));
                }
            }

            return true;
        }

        return false;
    }

    public KylinOrderExpressVo orderRouteDetail(String orderTicketsId) {
        List<KylinOrderExpressRouteVo> routeList = dataUtils.getOrderExpressRoute(orderTicketsId);
        KylinOrderExpressVo expressInfo = dataUtils.getOrderExpressInfo(orderTicketsId);
        expressInfo.setRouteList(routeList);
        return expressInfo;
    }
}