package cn.com.ty.lift.ud.operation.controller;

import cn.com.ty.lift.ud.common.DelReq;
import cn.com.ty.lift.ud.operation.controller.query.UserLevelRuleQuery;
import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.common.NotExamineDto;
import cn.com.ty.lift.ud.operation.mapper.entity.UserLevelRuleEntity;
import cn.com.ty.lift.ud.operation.mapper.entity.UserRebateRuleEntity;
import cn.com.ty.lift.ud.operation.service.IUserLevelRuleService;
import cn.com.ty.lift.ud.userInfo.service.IUserInfoService;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户角色表;API
 *
 * @author Xson
 * @date 2020-05-06
 */
@Api(tags = "用户角色表 API - 小陶")
@RestController
@RequestMapping("/api/userLevelRules")
public class UserLevelRuleController extends BaseController<UserLevelRuleEntity, UserLevelRuleQuery> {

    private final Logger log = LoggerFactory.getLogger(UserLevelRuleController.class);

    private @Autowired
    IUserLevelRuleService userLevelRuleService;

    private @Autowired
    IUserInfoService iUserInfoService;

    @Override
    protected IService<UserLevelRuleEntity> getService() {
        return userLevelRuleService;
    }


    /**
     * 重写修改
     *
     * @param request
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    @Override
    public RestResponse update(@RequestBody UserLevelRuleEntity request) {
        UserLevelRuleEntity OldEntity = userLevelRuleService.getById(request.getId());
        if(null == request.getRoleCode()){
            return RestResponse.fail("角色编码不能为空");
        }
        //role_code不能重复
        UserLevelRuleEntity byCode = userLevelRuleService.getByCode(request.getRoleCode());
        if(null != byCode && !byCode.getId().equals(request.getId()) ){
            return RestResponse.fail("角色编码不能重复");
        }
        boolean updateFlag = getService().updateById(request);
        UserLevelRuleEntity e = getService().getById(request.getId());
        if (updateFlag) {
            //修改成功，要同时修改 user_info 表的 user_level_name
            iUserInfoService.updateUserLevelName(request.getName(),OldEntity.getName());
            return RestResponse.success(null, "修改成功");
        } else {
            return RestResponse.fail("修改失败");
        }
    }

    /**
     * 新增提交
     *
     * @param entity
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增提交")
    @Override
    public RestResponse create(@RequestBody UserLevelRuleEntity entity) {
        if(null == entity.getName()){
            return RestResponse.fail("角色名称不能为空");
        }
        if(null == entity.getRoleCode()){
            return RestResponse.fail("角色编码不能为空");
        }
        if(null == entity.getVipFlag()){
            return RestResponse.fail("是否付费不能为空");
        }
        //role_code不能重复
        UserLevelRuleEntity byCode = userLevelRuleService.getByCode(entity.getRoleCode());
        if(null != byCode ){
            return RestResponse.fail("角色编码不能重复");
        }
        return userLevelRuleService.create(entity);
    }


    @GetMapping("/getVipUserLevel")
    @ApiOperation(value = "获取所有的vip角色")
    public RestResponse getVipUserLevel() {
        List<UserLevelRuleEntity>  list=  userLevelRuleService.getVipUserLevel();
        return RestResponse.success(list,"查询成功");
    }

    /**
     * 批量删除+逻辑判断
     * @param ids
     * @return
     */
    @DeleteMapping("/delete-many")
    @ApiOperation(value = "批量删除+逻辑判断")
    public RestResponse deleteBath(@RequestBody DelReq ids) {
        return userLevelRuleService.deleteByIds(ids.getIds());

    }

    /**
     * 查询所有的付费角色,以及专家
     *
     * @return
     */
    @PostMapping("/getNotSetRole")
    @ApiOperation(value = "查询所有的付费角色,以及专家")
    public List<UserLevelRuleEntity> getNotSetRole() {
        return userLevelRuleService.getNotSetRole();
    }

}