package com.sqx.modules.common.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.sqx.common.utils.Result;
import com.sqx.modules.artificer.dao.UserRechargeDao;
import com.sqx.modules.common.dao.ArtificerPartitioningDetailsMapper;
import com.sqx.modules.common.dao.CommonInfoDao;
import com.sqx.modules.common.entity.ArtificerPartitioningDetails;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.enums.CommonEnum;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.common.utils.CommonConfigUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author fang
 * @date 2020/7/8
 */
@Service
public class CommonInfoServiceImpl extends ServiceImpl<CommonInfoDao, CommonInfo> implements CommonInfoService {

    private final CommonInfoDao commonInfoDao;
    private final UserRechargeDao userRechargeDao;
    private final ArtificerPartitioningDetailsMapper artificerPartitioningDetailsMapper;

    @Autowired
    public CommonInfoServiceImpl(CommonInfoDao commonInfoDao,UserRechargeDao userRechargeDao,ArtificerPartitioningDetailsMapper artificerPartitioningDetailsMapper) {
        this.commonInfoDao = commonInfoDao;
        this.userRechargeDao = userRechargeDao;
        this.artificerPartitioningDetailsMapper = artificerPartitioningDetailsMapper;
    }

    @Override
    public Result update(CommonInfo commonInfo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        commonInfo.setCreateAt(sdf.format(now));
        commonInfoDao.updateById(commonInfo);
        return Result.success();
    }


    @Override
    public CommonInfo findOne(int id) {
        return commonInfoDao.findOne(id);
    }

    @Override
    public Result delete(long id) {
        commonInfoDao.deleteById(id);
        return Result.success();
    }


    @Override
    public Result updateBody(CommonInfo commonInfo) {
        commonInfoDao.updateById(commonInfo);
        return Result.success();
    }

    @Override
    public Result findByType(Integer type) {
        return Result.success().put("data",commonInfoDao.findOne(type));
    }

    @Override
    public Result findByTypeAndCondition(String condition) {
        return Result.success().put("data", getListByCondition(condition));
    }

    @Override
    public Result rules(String type) {
        return Result.success().put("data", artificerPartitioningDetailsMapper.selectList(Wrappers.<ArtificerPartitioningDetails>lambdaQuery()
                .eq(ArtificerPartitioningDetails::getClassifyId, type)));
    }

    @Override
    public Result rulesUpd(ArtificerPartitioningDetails artificerPartitioningDetails) {
        artificerPartitioningDetailsMapper.updateById(artificerPartitioningDetails);
        return Result.success();
    }

    @Override
    public CommonInfo findOneByEnum(CommonEnum commonEnum) {
        return findOne(commonEnum.getKey());
    }

    @Override
    public List<CommonInfo> getListByCondition(String condition) {
        return commonInfoDao.findByCondition(condition);
    }

    @Override
    public List<CommonInfo> getListByConditionLike(String condition) {
        QueryWrapper<CommonInfo> qw = new QueryWrapper<>();
        qw.lambda().like(CommonInfo::getConditionFrom,condition);
        return list(qw);
    }

    @Override
    public List<CommonInfo> getListByTypes(Integer[] values) {
        QueryWrapper<CommonInfo> qw = new QueryWrapper<>();
        qw.lambda().in(CommonInfo::getType,values);
        return list(qw);
    }

    @Override
    @Transactional(rollbackFor = {java.lang.Exception.class})
    public boolean updateGroup(List<CommonInfo> infos) {
        if(infos != null && !infos.isEmpty()){
            //保证有初始数据，这里不做新增
            return updateBatchById(infos);
        }
        return false;
    }

    @Override
    public List<CommonInfo> getV3OrderForm() {
        return getListByTypes(CommonEnum.GroupKeyEnum.V3_ORDER_FORM.getValues());
    }

    @Override
    public Map<Integer, CommonInfo> getMapByCondition(String condition) {
        List<CommonInfo> commonInfoList = commonInfoDao.findByCondition(condition);
        Map<Integer, CommonInfo> commonInfoMap = Maps.newHashMap();
        commonInfoList.forEach(x -> commonInfoMap.put(x.getType(), x));
        return commonInfoMap;
    }

    @Override
    public CommonInfo isCheckChangeOrder(Date businessTime, Date nowTime) {
        long bnDiffDay = DateUtil.between(nowTime, businessTime, DateUnit.MINUTE, false);
        Map<Integer, CommonInfo> configMap = getMapByCondition(CommonEnum.GroupConditionEnum.V3_ORDER_FORM.getValue());

        CommonInfo defCommonInfo = new CommonInfo();

        if(bnDiffDay > 0) {
            //正数 开始前
            //距离服务开始n分钟以上免费取消  240
            CommonInfo v3OrderFormCancelNotStart01 = configMap.getOrDefault(CommonEnum.V3_ORDER_FORM_CANCEL_NOT_START_01.getKey(), defCommonInfo);
            Long v3OrderFormCancelNotStart01D = CommonConfigUtil.getLongValue(v3OrderFormCancelNotStart01.getValue());
            if(v3OrderFormCancelNotStart01D == null) v3OrderFormCancelNotStart01D = 240L;
            //距离服务开始前取消n-n分钟内，收取n元 90,240,10
            CommonInfo v3OrderFormCancelNotStart02 = configMap.getOrDefault(CommonEnum.V3_ORDER_FORM_CANCEL_NOT_START_02.getKey(), defCommonInfo);
            String[] v3OrderFormCancelNotStart02A = CommonConfigUtil.getArrayValue(v3OrderFormCancelNotStart02.getValue());
            if(v3OrderFormCancelNotStart02A == null) v3OrderFormCancelNotStart02A = new String[]{ "90","240","10" };
            Long v3OrderFormCancelNotStart02D = CommonConfigUtil.getLongValue(v3OrderFormCancelNotStart02A[0]);
            //距离服务开始前取消少于n分钟内，收取n元 90,30
            CommonInfo v3OrderFormCancelNotStart03 = configMap.getOrDefault(CommonEnum.V3_ORDER_FORM_CANCEL_NOT_START_03.getKey(), defCommonInfo);
            String[] v3OrderFormCancelNotStart03A = CommonConfigUtil.getArrayValue(v3OrderFormCancelNotStart03.getValue());
            if(v3OrderFormCancelNotStart03A == null) v3OrderFormCancelNotStart03A = new String[]{ "90","30" };
            Long v3OrderFormCancelNotStart03D = CommonConfigUtil.getLongValue(v3OrderFormCancelNotStart03A[0]);

            if(bnDiffDay >= v3OrderFormCancelNotStart01D){
                //外面的
                return v3OrderFormCancelNotStart01;
            } else if(bnDiffDay >= v3OrderFormCancelNotStart02D && bnDiffDay < v3OrderFormCancelNotStart01D  ){
                return v3OrderFormCancelNotStart02;
            } else if(bnDiffDay < v3OrderFormCancelNotStart03D){
                return v3OrderFormCancelNotStart03;
            }
        } else {
            //负数，开始后
            long bnDiffMinute = DateUtil.between(businessTime, nowTime, DateUnit.MINUTE, false);
            //服务开始n分钟内取消订单，扣项目费用n%，车费全扣n%； 15,50,100
            CommonInfo v3OrderFormCancelStart01 = configMap.getOrDefault(CommonEnum.V3_ORDER_FORM_CANCEL_START_01.getKey(), defCommonInfo);
            String[] v3OrderFormCancelStart01A = CommonConfigUtil.getArrayValue(v3OrderFormCancelStart01.getValue());
            if(v3OrderFormCancelStart01A == null) v3OrderFormCancelStart01A = new String[]{ "15","50","100" };
            Long v3OrderFormCancelStart01I = CommonConfigUtil.getLongValue(v3OrderFormCancelStart01A[0]);
            //服务开始n分钟后不可以取消订单 15
            CommonInfo v3OrderFormCancelStart02 = configMap.getOrDefault(CommonEnum.V3_ORDER_FORM_CANCEL_START_02.getKey(), defCommonInfo);
            Long v3OrderFormCancelStart02I = CommonConfigUtil.getLongValue(v3OrderFormCancelStart02.getValue());
            if(v3OrderFormCancelStart02I == null) v3OrderFormCancelStart02I = 15L;

            if(bnDiffMinute <= v3OrderFormCancelStart01I) {
                return v3OrderFormCancelStart01;
            } else if(bnDiffMinute > v3OrderFormCancelStart02I){
                return v3OrderFormCancelStart02;
            }
        }
        return defCommonInfo;
    }

    @Override
    public boolean isCheckEditOrder(Date businessTime, Date nowTime) {
        long bnDiffDay = DateUtil.between(nowTime, businessTime, DateUnit.MINUTE, false);
        Map<Integer, CommonInfo> configMap = getMapByCondition(CommonEnum.GroupConditionEnum.V3_ORDER_FORM.getValue());

        //距离服务开始前n分钟可修改  120
        CommonInfo v3OrderFormEditNotStart01 = configMap.get(CommonEnum.V3_ORDER_FORM_EDIT_NOT_START_01.getKey());

        Long v3OrderFormEditNotStart01I = CommonConfigUtil.getLongValue(v3OrderFormEditNotStart01.getValue());
        if(v3OrderFormEditNotStart01I == null) v3OrderFormEditNotStart01I = 120L;

        if(bnDiffDay >= v3OrderFormEditNotStart01I) {
            return true;
        }else{
            return false;
        }

    }

}