package com.jeequan.jeepay.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.db.entity.*;
import com.jeequan.jeepay.service.mapper.*;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2024-04-19
 */
@Service
public class RouteManageService extends ServiceImpl<RouteManageMapper, RouteManage>{

    @Autowired private MchStoreMapper mchStoreMapper;
    @Autowired private MchApplymentMapper mchApplymentMapper;
    @Autowired private RouteMapper routeMapper;
    @Autowired private RouteRecordMapper routeRecordMapper;
    /**
     * 新增保存
     * @param entity
     */
    public void addSave(RouteManage entity) {
        entity.preCheck();
        Route route = routeMapper.selectById(entity.getRouteId());
        if(route == null){
            throw new BizException("非法的路由策略信息");
        }
        List<RouteManage> batchList = new ArrayList<>();
        for (String storeId:entity.getStoreIdList()) {
            RouteManage routeManage = new RouteManage();
            MchStore mchStore = mchStoreMapper.selectById(storeId);
            MchApplyment applyment = mchApplymentMapper.selectById(mchStore.getMchApplyId());
            routeManage.setRouteId(entity.getRouteId());
            routeManage.setStoreId(storeId);
            routeManage.setApplyId(mchStore.getMchApplyId());
            routeManage.setMchNo(entity.getMchNo());
            routeManage.setAppid(applyment.getAutoConfigMchAppId());
            routeManage.setIfCode(applyment.getIfCode());
            routeManage.setIsvNo(applyment.getIsvNo());
            Byte isPayType = route.getSeniorStrategy().getIsPayType();
            List<String> payTypeList = Lists.newArrayList(CS.PAY_WAY_CODE_TYPE.WECHAT, CS.PAY_WAY_CODE_TYPE.ALIPAY);
            if(CS.YES == isPayType){
                payTypeList = route.getSeniorStrategy().getPayTypeValue();
            }
            routeManage.setPayType(payTypeList);
            ArrayList<String> amtRangeList = Lists.newArrayList("1","5000000");
            routeManage.setAmtRange(amtRangeList);
            ArrayList<String> timeRange = Lists.newArrayList("00:00","23:59");
            routeManage.setTimeRange(timeRange);
            routeManage.setSettleType(applyment.getSettlementType());
            batchList.add(routeManage);
        }
        try {
            saveBatch(batchList);
        }catch (DuplicateKeyException e){
            throw new BizException("绑定的门店中包含已被绑定的门店数据");
        }
    }

    /**
     * 分页查询
     * @param page
     * @param entity
     * @return
     */
    public IPage<RouteManage> pageList(Page<RouteManage> page, RouteManage entity) {
        Date[] dateRange = entity.buildQueryDateRange();
        return baseMapper.pageList(page,entity,dateRange);
    }

    /**
     * 根据路由号和状态查询
     * @param routeId
     * @param state
     * @return
     */
    public List<RouteManage> listByRouteId(String routeId,byte state) {
        return baseMapper.listByRouteId(routeId,state);
    }

    /**
     * 加载路由数据
     * @param routeManage
     * @param payOrder
     */
    public void upRouteData(RouteManage routeManage, PayOrder payOrder) {
        RouteRecord routeRecord = routeManage.getRouteRecord();
        if(routeRecord != null){
            routeRecord.setOrderId(payOrder.getPayOrderId());
            routeRecord.setStoreId(routeManage.getStoreId());
            routeRecord.setApplyId(routeManage.getApplyId());
            routeRecordMapper.insert(routeRecord);
        }
        upStoreRouteData(payOrder.getFindAmt(),payOrder.getStoreId(),routeManage.getRouteId(),false);
    }

    /**
     * 更新门店路由数据
     * @param amount
     * @param storeId
     * @param isSucc 是否更新成功数据
     */
    public void upStoreRouteData(Long amount, String storeId,String routeId, boolean isSucc) {
        if(isSucc){
            baseMapper.updateSuccData(amount,storeId);
            routeMapper.updateSuccData(amount,routeId);
        }else{
            baseMapper.updateTotalData(amount,storeId);
            routeMapper.updateTotalData(amount,routeId);
        }
    }
}
