/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.distribution.comment.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.Json;
import com.yami.shop.distribution.comment.dao.DistributionLevelAuditingMapper;
import com.yami.shop.distribution.comment.dao.DistributionLevelMapper;
import com.yami.shop.distribution.comment.dao.DistributionLevelSetMapper;
import com.yami.shop.distribution.comment.dao.DistributionUserMapper;
import com.yami.shop.distribution.comment.model.DistributionLevel;
import com.yami.shop.distribution.comment.model.DistributionLevelSet;
import com.yami.shop.distribution.comment.service.DistributionLevelSetService;
import com.yami.shop.distribution.comment.vo.DistributionLevelConditionDataVO;
import com.yami.shop.distribution.comment.vo.DistributionLevelConditionsSwitchVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @author lgh on 2019/04/04.
 */
@Service
public class DistributionLevelSetServiceImpl extends ServiceImpl<DistributionLevelSetMapper, DistributionLevelSet> implements DistributionLevelSetService {

    @Autowired
    private DistributionLevelSetMapper distributionLevelSetMapper;

    @Autowired
    private DistributionLevelMapper distributionLevelMapper;

    @Autowired
    private DistributionUserMapper distributionUserMapper;

    @Autowired
    private DistributionLevelAuditingMapper distributionLevelAuditingMapper;


    @Override
     @Cacheable(cacheNames = "DistributionLevelSet", key = "'0'")
    public DistributionLevelSet getByShopId() {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        DistributionLevelSet distributionLevelSet = distributionLevelSetMapper.getDistributionLevelSetAndLevelByShopId(shopId);
//        if (distributionLevelSet == null) {
//            //如果获取为空,则创建一条初始数据
//            return createInitialLevelSet(shopId);
//        }
        List<DistributionLevel> distributionLevels = distributionLevelMapper.geyByShopId(shopId);
        for (DistributionLevel distributionLevel : distributionLevels) {
//            distributionLevel.setDistributionLevelConditionDataVO(Json.parseObject(distributionLevel.getConditionData(), DistributionLevelConditionDataVO.class));

            distributionLevel.setDistributionLevelConditionDataVO(JSONUtil.toBean(distributionLevel.getConditionData(), DistributionLevelConditionDataVO.class));
        }

        distributionLevelSet.setDistributionLevels(distributionLevels);
        distributionLevelSet.setLevelSetConditionsSwitch(Json.parseObject(distributionLevelSet.getLevelSetConditionsSwitchJson(), DistributionLevelConditionsSwitchVO.class));
        return distributionLevelSet;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveByDistributionLevelSet(DistributionLevelSet distributionLevelSet) {
        //遍历插入等级
        for (DistributionLevel distributionLevel : distributionLevelSet.getDistributionLevels()) {
            distributionLevel.setShopId(distributionLevelSet.getShopId());
            distributionLevelMapper.insert(distributionLevel);
        }
        //插入等级设置
        distributionLevelSetMapper.insert(distributionLevelSet);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "DistributionLevelSet", key = "'0'")
    public void updateByDistributionLevelSet(DistributionLevelSet distributionLevelSet) {
        Long shopId = distributionLevelSet.getShopId();

        // 删除之前的所有等级
        distributionLevelMapper.delete(new LambdaUpdateWrapper<DistributionLevel>().eq(DistributionLevel::getShopId, shopId));


        List<DistributionLevel> distributionLevels = distributionLevelSet.getDistributionLevels();
        //根据level进行排序

        distributionLevels.sort(Comparator.comparing(DistributionLevel::getLevel));

        int level = 0;
        //遍历判断插入新的等级
        for (DistributionLevel distributionLevel : distributionLevels) {
            //修正等级避免出现跨等级
            distributionLevel.setLevel(++level);
            distributionLevel.setShopId(shopId);
            distributionLevelMapper.insert(distributionLevel);
        }

        //将分销员的等级替换掉
        distributionUserMapper.replaceLevel(shopId, level);
        //将等级申请中失效的等级申请状态改为失效
        distributionLevelAuditingMapper.updateStateByShopId(shopId);

        //更新等级设置
        distributionLevelSetMapper.updateById(distributionLevelSet);
    }

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public DistributionLevelSet createInitialLevelSet(Long shopId) {
//        DistributionLevelSet distributionLevelSet = new DistributionLevelSet();
//        distributionLevelSet.setShopId(shopId);
//        distributionLevelSet.setLevelSetConditionsSwitch(new DistributionLevelConditionsSwitchVO());
//        distributionLevelSet.setLevelSetConditionsSwitchJson(Json.toJsonString(new DistributionLevelConditionsSwitchVO()));
//        distributionLevelSet.setAwardProportion(0);
//        distributionLevelSet.setAwardSwitch(0);
//        distributionLevelSet.setParentAwardSwitch(0);
//        distributionLevelSet.setIsAutoAuditing(0);
//
//        DistributionLevel distributionLevel = new DistributionLevel();
//        distributionLevel.setName("默认等级");
//        distributionLevel.setLevel(1);
//        distributionLevel.setShopId(shopId);
//        distributionLevel.setAwardNumber(0.0);
//        distributionLevel.setParentAwardNumber(0.0);
//        distributionLevel.setConditionData(Json.toJsonString(new DistributionLevelConditionDataVO()));
//
//        distributionLevelSetMapper.insert(distributionLevelSet);
//        distributionLevelMapper.insert(distributionLevel);
//        distributionLevelSet.setDistributionLevels(Arrays.asList(distributionLevel));
//        return distributionLevelSet;
//    }
}
