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.UserRebateRuleQuery;
import cn.com.ty.lift.ud.operation.mapper.UserRebateRuleMapper;
import cn.com.ty.lift.ud.operation.mapper.entity.UserRebateRuleEntity;
import cn.com.ty.lift.ud.operation.service.IUserRebateRuleService;
import cn.com.ty.lift.ud.tableDict.service.ITableDictService;
import cn.com.ty.lift.ud.userInfo.mapper.entity.UserInfoEntity;
import cn.com.ty.lift.ud.userInfo.service.IUserInfoService;
import cn.com.xwy.boot.web.dto.RestResponse;
import cn.com.xwy.util.collection.ListUtil;
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-28
 */
@Service
@Transactional
public class UserRebateRuleServiceImpl extends ServiceImpl<UserRebateRuleMapper,UserRebateRuleEntity>
    implements IUserRebateRuleService {


    public static final int EXAMINEFLAG = 1;

    @Autowired
    private UserRebateRuleMapper UserRebateRuleMapper;

    @Autowired
    private ITableDictService iTableDictService;

    @Autowired
    private IUserInfoService iUserInfoService;

    /**
     * 审批
     *
     * @param neDto
     * @return
     */
    @Override
    public RestResponse examineById(NotExamineDto neDto) {
        if (EXAMINEFLAG == neDto.getExamineFlag()) {
            //如果存在相同的角色设置，提示先删除才能审核通过，不然存在相同的2个版本
            UserRebateRuleEntity entity = UserRebateRuleMapper.selectById(neDto.getId());
            if(null != entity){
                //根据 角色ID查询是否存在已经审核通过的版本
                QueryWrapper<UserRebateRuleEntity> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("role_id", entity.getRoleId());
                queryWrapper.eq("examine_flag", 1);
                UserRebateRuleEntity ruleEntity = UserRebateRuleMapper.selectOne(queryWrapper);
                if(ruleEntity == null){
                    //审核通过
                    UserRebateRuleMapper.examineById(neDto.getId());
                    return RestResponse.success(null, "审批通过");
                }else{
                    return RestResponse.fail("存在角色相同的设置，先删除才能审核通过");
                }
            }else{
                return RestResponse.fail("当前ID对应的数据不存在");
            }
        } else {
            //修改状态为 驳回，并加上驳回理由
            UserRebateRuleMapper.updateInfoById(neDto.getId(), neDto.getNotExamineReason());
            return RestResponse.fail("审批驳回");
        }
    }

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

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


    /**
     * 根据app角色查询返利信息
     * @param userLevel 用户角色ID
     * @return
     */
    @Override
    public UserRebateRuleEntity getRebateByUserLevel(Long userLevel) {
        return UserRebateRuleMapper.getRebateByUserLevel(userLevel);
    }

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


}
