package cn.com.countrygarden.bi.main.controller.wcx;

import cn.com.countrygarden.bi.main.constant.wcx.WcxConstant;
import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dto.wcx.WcxRoleDto;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.service.wcx.WcxRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 微创新角色控制层
 * 新增角色
 * 删除角色
 * 修改角色
 * 修改权重比例
 * 查询分页的角色
 * @author ferren
 */
@RestController
@RequestMapping("/api/wcx/role")
public class WcxRoleController {

    @Autowired
    private WcxRoleService wcxRoleService;

    @PostMapping
    public Result create(@RequestBody WcxRoleDto wcxRoleDto) {
        try {
            wcxRoleService.checkRoleDto(wcxRoleDto);
            wcxRoleService.save(wcxRoleDto);
        } catch (ServiceException e) {
            e.printStackTrace();
            return Response.responseFail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Response.responseFail("请刷新页面后重试,或联系管理员");
        }
        return Response.response(WcxConstant.DEAL_SUCCESS);
    }

    @DeleteMapping("/{roleId}")
    public Result delete(@PathVariable String roleId){
        try {
            wcxRoleService.deleteById(roleId);
        } catch (ServiceException e) {
            return Response.responseFail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Response.responseFail("请刷新页面后重试,或联系管理员");
        }
        return Response.response(WcxConstant.DEAL_SUCCESS);
    }

    @PutMapping("/{roleId}")
    public Result update(@PathVariable String roleId, @RequestBody WcxRoleDto wcxRoleDto){
        try {
            wcxRoleService.checkRoleDto(wcxRoleDto);
            wcxRoleDto.setRoleId(roleId);
            wcxRoleService.update(wcxRoleDto);
        } catch (ServiceException e) {
            return Response.responseFail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Response.responseFail("请刷新页面后重试,或联系管理员");
        }
        return Response.response(WcxConstant.DEAL_SUCCESS);
    }

    @GetMapping("/pageList")
    public Result selectPageList(WcxRoleDto wcxRoleDto){
        PageData pageData = wcxRoleService.selectPageList(wcxRoleDto);
        return Response.response(pageData);
    }

    /**
     * 更新领导点赞比例
     * @param weightRatio 点赞比例
     * @return
     */
    @PutMapping("/weightRatio")
    public Result updateThumsRoleOfLeader(@RequestParam Integer weightRatio){
        if (weightRatio == null || weightRatio <= 0) {
            throw new ServiceException("点赞比例不能为空,或小于0");
        }
        wcxRoleService.updateWeightRatio(weightRatio, WcxConstant.ROLE_IS_LEADER);
        return Response.response(WcxConstant.DEAL_SUCCESS);
    }

    /**
     * 查询权重比例
     * @return
     */
    @GetMapping("/weightRatio")
    public Result selectWeightRatio(){
        int weightRatio = wcxRoleService.selectWeightRatioByRoleType(WcxConstant.ROLE_IS_LEADER);
        return Response.response(weightRatio);
    }
}
