package cn.com.ty.lift.ud.operation.service.impl;

import cn.com.ty.lift.ud.common.NotExamineDto;
import cn.com.ty.lift.ud.operation.controller.query.VipFeeSettingQuery;
import cn.com.ty.lift.ud.operation.mapper.UserLevelRuleMapper;
import cn.com.ty.lift.ud.operation.mapper.VipFeeSettingMapper;
import cn.com.ty.lift.ud.operation.mapper.entity.UserRebateRuleEntity;
import cn.com.ty.lift.ud.operation.mapper.entity.VipFeeSettingEntity;
import cn.com.ty.lift.ud.operation.service.IUserRebateRuleService;
import cn.com.ty.lift.ud.operation.service.IVipFeeSettingService;
import cn.com.ty.lift.ud.tableDict.service.ITableDictService;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 会费设置;-ServiceImpl
 *
 * @author Xson
 * @date 2020-06-19
 */
@Service
@Transactional
public class VipFeeSettingServiceImpl extends ServiceImpl<VipFeeSettingMapper,VipFeeSettingEntity>
    implements IVipFeeSettingService {


    public static final int EXAMINEFLAG = 1;

    @Autowired
    private VipFeeSettingMapper vipFeeSettingMapper;

    @Autowired
    private IUserRebateRuleService iUserRebateRuleService;


    /**
     * 审批
     *
     * @param neDto
     * @return
     */
    @Override
    public RestResponse examineById(NotExamineDto neDto) {
        if (EXAMINEFLAG == neDto.getExamineFlag()) {
            //如果存在相同的角色设置，提示先删除才能审核通过，不然存在相同的2个版本
            VipFeeSettingEntity entity = vipFeeSettingMapper.selectById(neDto.getId());
            if(null != entity){
                //如果当前角色是收费角色，且没有设置返利信息，不能设置会费
                UserRebateRuleEntity userLevel = iUserRebateRuleService.getRebateByUserLevel(entity.getUserLevelId());
                if(null == userLevel){
                    return RestResponse.fail("请首先设置该角色的返利信息");
                }
                //根据 角色ID查询是否存在已经审核通过的版本
                QueryWrapper<VipFeeSettingEntity> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("period", entity.getPeriod());
                queryWrapper.eq("user_level_id", entity.getUserLevelId());
                queryWrapper.eq("examine_flag", 1);
                VipFeeSettingEntity ruleEntity = vipFeeSettingMapper.selectOne(queryWrapper);
                if(ruleEntity == null){
                    //审核通过
                    vipFeeSettingMapper.examineById(neDto.getId());
                    return RestResponse.success(null, "审批通过");
                }else{
                    return RestResponse.fail("同一种vip的有效期时长不能重复，先删除才能审核通过");
                }
            }else{
                return RestResponse.fail("当前ID对应的数据不存在");
            }
        } else {
            //修改状态为 驳回，并加上驳回理由
            vipFeeSettingMapper.updateInfoById(neDto.getId(), neDto.getNotExamineReason());
            return RestResponse.fail(null, "审批驳回");
        }
    }

    /**
     * 提交操作
     *
     * @param id
     * @return
     */
    @Override
    public RestResponse submit(Long id) {
        int submit = vipFeeSettingMapper.submit(id);
        if (submit == 1) {
            return RestResponse.success(null, "提交成功");
        } else {
            return RestResponse.fail(null, "提交失败");
        }
    }

    /**
     * 根据有效期和app角色ID查询会费
     * @param period
     * @return
     */
    @Override
    public VipFeeSettingEntity getByPeriodAndLevel(Integer period,Long userLevelId) {
        return vipFeeSettingMapper.getByPeriodAndLevel(period,userLevelId);
    }

    /**
     * 新增提交
     *
     * @param entity
     * @return
     */
    @Override
    public RestResponse create(VipFeeSettingEntity entity) {
        //设置会费
        Long id = entity.getId();
        if (null == id) {
            //直接点的提交按钮
            entity.setExamineFlag(0);
            int insert = vipFeeSettingMapper.insert(entity);
            if (insert == 1) {
                return RestResponse.success(null, "新建成功");
            } else {
                return RestResponse.fail("新建失败");
            }
        } else {
            //先保存再提交,修改状态为未审核 0
            return submit(id);
        }
    }

    /**
     * 根据用户等级id查询会费设置信息
     * @param userLevelId
     * @return
     */
    @Override
    public RestResponse getAll(Integer userLevelId) {
        List<VipFeeSettingEntity> list = list(new QueryWrapper<VipFeeSettingEntity>()
                .eq("user_level_id", userLevelId).eq("examine_flag", 1)
        );
        return RestResponse.success(list, "查询成功");
    }

    /**
     * 后台分页查询
     * @param query
     * @return
     */
    @Override
    public RestResponse selectByExample(VipFeeSettingQuery query) {
        IPage page = query.page();
        List<VipFeeSettingEntity> list = vipFeeSettingMapper.selectByQuery(query.condition(), query);
        page.setRecords(list);
        int total = vipFeeSettingMapper.countByQuery(query.condition());
        page.setTotal(total);
        return RestResponse.success(page, "Select successfully");
    }
}
