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

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.common.NotExamineDto;
import cn.com.ty.lift.ud.operation.mapper.entity.UserLevelRuleEntity;
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 java.util.ArrayList;
import java.util.List;

import cn.com.xwy.util.collection.ListUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.ty.lift.ud.operation.mapper.UserLevelRuleMapper;
import cn.com.ty.lift.ud.operation.service.IUserLevelRuleService;

/**
 * 用户等级规则;-ServiceImpl
 *
 * @author Xson
 * @date 2020-05-06
 */
@Service
@Transactional
public class UserLevelRuleServiceImpl extends ServiceImpl<UserLevelRuleMapper, UserLevelRuleEntity>
        implements IUserLevelRuleService {

    public static final int EXAMINEFLAG = 1;

    @Autowired
    private UserLevelRuleMapper userLevelRuleMapper;

    @Autowired
    private ITableDictService iTableDictService;

    @Autowired
    private IUserInfoService iUserInfoService;

    @Autowired
    private IUserRebateRuleService iUserRebateRuleService;


    /**
     * 新增提交
     *
     * @param entity
     * @return
     */
    @Override
    public RestResponse create(UserLevelRuleEntity entity) {
        //role_code不能重复
        UserLevelRuleEntity byCode = userLevelRuleMapper.getByCode(entity.getRoleCode());
        if(null != byCode){
            return RestResponse.fail("角色编码不能重复");
        }
        Long id = entity.getId();
        if (null == id) {
            //直接点的提交按钮
            int insert = userLevelRuleMapper.insert(entity);
            if (insert == 1) {
                return RestResponse.success(null, "新建成功");
            } else {
                return RestResponse.fail("新建失败");
            }
        } else {
            return null;
        }
    }

    /**
     * 根据编码查询app角色ID
     * @param personVip
     * @return
     */
    @Override
    public UserLevelRuleEntity getByCode(String personVip) {
        return userLevelRuleMapper.getByCode(personVip);
    }

    /**
     * 获取所有的vip角色
     * @return
     */
    @Override
    public List<UserLevelRuleEntity> getVipUserLevel() {
        List<UserLevelRuleEntity> levelRuleList = userLevelRuleMapper.getVipUserLevel();
        return levelRuleList;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public RestResponse deleteByIds(List<Long> ids) {
        for(int k = 0;k<ids.size();k++){
            //判断是否有用户使用该角色
            List<UserInfoEntity> byUserLevelList = iUserInfoService.getByUserLevel(ids.get(k));
            if(ListUtil.isNotEmpty(byUserLevelList)){
                  return RestResponse.fail("包含正在使用的用户等级，不能删除");
            }else{
                //删除该用户等级
                userLevelRuleMapper.deleteById(ids.get(k));
            }
        }
        return RestResponse.success(null,"删除成功");
    }

    /**
     * 查询所有的付费角色,以及专家
     * @return
     */
    @Override
    public List<UserLevelRuleEntity> getNotSetRole() {
        List<UserLevelRuleEntity> list = new ArrayList<>();
        List<UserLevelRuleEntity> vipRoleList =   userLevelRuleMapper.getNotSetRole();
        UserLevelRuleEntity expert = userLevelRuleMapper.getByCode("EXPERT");

        if(ListUtil.isNotEmpty(vipRoleList) ){
            list.addAll(vipRoleList);
        }
        if(expert != null ){
            list.add(expert);
        }
        return list;
    }

}
