package com.ho.business.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.ho.business.entity.IncomeRateMapping;
import com.ho.business.entity.IncomeTemplateType;
import com.ho.business.entity.RateTemplate;
import com.ho.business.feignclient.UserFeignClient;
import com.ho.business.mapper.IncomeMappingMapper;
import com.ho.business.service.IncomeMappingService;
import com.ho.common.tools.entity.SimpleUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IncomeMappingServiceImpl implements IncomeMappingService {

    @Autowired
    private IncomeMappingMapper incomeMappingMapper;

    @Autowired
    Snowflake snowflake;

    @Autowired
    UserFeignClient userFeignClient;


    @Override
    public List<RateTemplate> selectRateTemplates(RateTemplate rateTemplate)
    {
        List<RateTemplate> rateTemplates = incomeMappingMapper.selectRateTemplates(rateTemplate);
        return rateTemplates;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String,Object> insertRateTemplate(RateTemplate rateTemplate, SimpleUser simpleUser)
    {
        Map<String,Object> result = new HashMap<>(0);
        if( rateTemplate == null){
            result.put("code","400");
            result.put("msg","模板内容不能为空!");
            return result;
        }
        int line = incomeMappingMapper.checkTemplateName(rateTemplate.getTemplateName(),rateTemplate.getStationId());
        if( line > 0 ){
            result.put("code","401");
            result.put("msg","模板名称不能相同,请确保模板名称唯一!");
            return result;
        }
        try {
            int i = 0;
            long id = snowflake.nextId();
            rateTemplate.setId(id);
            List<IncomeRateMapping> rateMappings = rateTemplate.getRateMappings();
            rateTemplate.setOperatorId(simpleUser.getUserId());
            rateTemplate.setCreateTime(new Date());
            incomeMappingMapper.insertRateTemplate(rateTemplate);
            i = incomeMappingMapper.insertRateMapping(rateMappings, id);
            if ( i > 0 ) {
                result.put("code","200");
                result.put("msg","保存成功!");
                return result;
            } else {
                result.put("code","201");
                result.put("msg","保存失败!");
                return result;
            }
        } catch (Exception e) {
            result.put("code","201");
            result.put("msg","保存失败!");
            return result;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String,Object> modifyRateTemplate(RateTemplate rateTemplate,SimpleUser simpleUser)
    {
        Map<String,Object> result = new HashMap<>(0);
        try {
            long templateId = rateTemplate.getId();
            int i = incomeMappingMapper.checkTotalTemplate(templateId);
            if( i > 0  )
            {
                result.put("code","201");
                result.put("msg","模板使用后收益未确认!不可编辑!");
                return result;
            }
            rateTemplate.setModifyId(simpleUser.getUserId());
            rateTemplate.setModifyTime(new Date());
            List<IncomeRateMapping> rateMappings = rateTemplate.getRateMappings();
            incomeMappingMapper.modifyRateTemplate(rateTemplate);
            incomeMappingMapper.deleteRateMapping(templateId);
            incomeMappingMapper.insertRateMapping(rateMappings,templateId);
            result.put("code","200");
            result.put("msg","操作成功!");
        } catch (Exception e) {
            result.put("code","400");
            result.put("msg","操作失败!");
        }
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String,Object> deleteRateTemplate(long id)
    {
        Map<String,Object> result = new HashMap<>(0);

        // 判断模板是否已经被试用,未使用模板才允许删除,否则给出提示
        int i = incomeMappingMapper.checkTemplateRate(id);
        if( i > 0 ){
            result.put("code","201");
            result.put("msg","模板已被使用!不可删除!");
            return result;
        }
        i = incomeMappingMapper.checkTemplateQRate(id);
        if( i > 0  ){
            result.put("code","201");
            result.put("msg","模板已被使用!不可删除!");
            return result;
        }
        incomeMappingMapper.deleteRateTemplate(id);
        incomeMappingMapper.deleteRateMapping(id);
        result.put("code","201");
        result.put("msg","操作成功!");
        return result;
    }

    @Override
    public List<IncomeTemplateType> getIncomeTemplateTypes()
    {
        return incomeMappingMapper.getIncomeTemplateTypes();
    }

    @Override
    public Map<String,Object> changeSwitch(RateTemplate rateTemplate)
    {
        Map<String,Object> result = new HashMap<>(0);
        if( rateTemplate != null ){

            int i = incomeMappingMapper.checkTotalTemplate(rateTemplate.getId());
            if( i > 0 && rateTemplate.getOpen() != 1 )
            {
                result.put("code","201");
                result.put("msg","模板使用后收益未确认!不可关闭!");
                return result;
            }

            // 判断需要设置的状态是否是开启,如果不是关闭不校验,直接执行
            if( rateTemplate.getOpen() != 1 ){
                int line = incomeMappingMapper.changeSwitch(rateTemplate);
                result.put("code",line>0?"200":"400");
                result.put("msg",line>0?"操作成功!":"操作失败!");
                return result;
            }
            // 判断是否有别的相同类型的模板已经打开,如果有则不允许再打开另外一个
            i = incomeMappingMapper.checkTemplateOpen(rateTemplate);
            if( i > 0 ){
                result.put("code","201");
                result.put("msg","同一种类型只允许启用一个!请关闭另一个模板后再试!");
                return result;
            }

            int line = incomeMappingMapper.changeSwitch(rateTemplate);
            result.put("code",line>0?"200":"400");
            result.put("msg",line>0?"操作成功!":"操作失败!");
            return result;
        }
        result.put("code","500");
        result.put("msg","参数不可为空!");
        return result;
    }
}
