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.WcxInnovationOtherDto;
import cn.com.countrygarden.bi.main.dto.wcx.WcxReviewOtherDto;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.sys.UserRoles;
import cn.com.countrygarden.bi.main.model.twerp.hm.TbSysUser;
import cn.com.countrygarden.bi.main.service.twerp.hm.TbSysUserService;
import cn.com.countrygarden.bi.main.service.wcx.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 举报
 */
@RestController
@RequestMapping("/api/wcx/wcxInfoReportManagement")
public class WcxInfoReportManagementController {
    @Autowired
    private WcxReviewOtherService wcxReviewOtherService;
    @Autowired
    private TbSysUserService tbSysUserService;
    @Autowired
    private WcxUserService wcxUserService;
    @Autowired
    private WcxInnovationOtherService wcxInnovationOtherService;
    @Autowired
    private WcxRoleService wcxRoleService;
    @Autowired
    private WcxRegionProjectService wcxRegionProjectService;

    /**
     * 获取创新数据列表
     *
     * @param condition
     * @return
     */
    @GetMapping("/wcxReviewOther/pageList")
    public Result selectInnovationPageList(WcxReviewOtherDto condition) {
        PageData pageData = wcxReviewOtherService.selectPageList(condition);
        return Response.response(pageData);
    }

    /**
     * 撤回举报内容
     *
     * @param wcxReviewOtherId
     * @return
     */
    @GetMapping("/wcxReviewOther/cancel")
    public Result cancelWcxReviewOther(String wcxReviewOtherId, HttpServletRequest request) {
        if (!StringUtils.hasText(wcxReviewOtherId)) {
            throw new ServiceException("标识wcxReviewOtherId不能为空");
        }
        AccessToken accessToken = wcxUserService.getAccessToken(request);
        String sysUserCode = accessToken.getUserCode();
        TbSysUser sysUser = tbSysUserService.findByUserCode(sysUserCode);
        wcxReviewOtherService.cancelReport(wcxReviewOtherId, sysUser.getUserCode(), sysUser.getUserName());
        return new Result().setResultMsg(WcxConstant.DEAL_SUCCESS);
    }

    /**
     * 屏蔽举报的评论
     *
     * @param wcxReviewOtherId
     * @return
     */
    @GetMapping("/wcxReviewOther/hide")
    public Result hidelWcxReviewOther(String wcxReviewOtherId, HttpServletRequest request) {
        AccessToken accessToken = wcxUserService.getAccessToken(request);
        String sysUserCode = accessToken.getUserCode();
        TbSysUser sysUser = tbSysUserService.findByUserCode(sysUserCode);
        wcxReviewOtherService.hideReport(wcxReviewOtherId, sysUser.getUserCode(), sysUser.getUserName());
        return new Result().setResultMsg(WcxConstant.DEAL_SUCCESS);
    }

    /**
     * 获取创新数据列表
     *
     * @param condition
     * @return
     */
    @GetMapping("/wcxInnovationOther/pageList")
    public Result selectInnovationPageList(WcxInnovationOtherDto condition, HttpServletRequest request) {
        AccessToken accessToken = wcxUserService.getAccessToken(request);
        String sysUserCode = accessToken.getUserCode();
        List<UserRoles> userRoles = wcxRoleService.selectSysRoleBySysUserCode(sysUserCode);
        String maxRole = wcxRoleService.getMaxRole(userRoles);
        if ("Z".equals(maxRole)) {

        } else if("0".equals(maxRole)){
            String erpRegionName = wcxRegionProjectService.selectErpRegionName(sysUserCode);
            if (!StringUtils.hasText(erpRegionName)) {
                throw new ServiceException("关联不上登录用户所在区域");
            }
            condition.setInnoRegionName(erpRegionName);
        } else {
            condition.setInnoBaseline(maxRole);
        }
        PageData pageData = wcxInnovationOtherService.selectPageList(condition);

        return Response.response(pageData);
    }

    /**
     * 撤回举报内容
     *
     * @param wcxInnovationOtherId
     * @return
     */
    @GetMapping("/wcxInnovationOther/cancel")
    public Result cancelWcxInnovationOther(String wcxInnovationOtherId, HttpServletRequest request) {
        if (!StringUtils.hasText(wcxInnovationOtherId)) {
            throw new ServiceException("标识wcxInnovationOtherId不能为空");
        }
        AccessToken accessToken = wcxUserService.getAccessToken(request);
        String sysUserCode = accessToken.getUserCode();
        TbSysUser sysUser = tbSysUserService.findByUserCode(sysUserCode);
        wcxInnovationOtherService.cancelReport(wcxInnovationOtherId, sysUser.getUserCode(), sysUser.getUserName());
        return new Result().setResultMsg(WcxConstant.DEAL_SUCCESS);
    }

    /**
     * 屏蔽举报的评论
     *
     * @param wcxInnovationOtherId
     * @return
     */
    @GetMapping("/wcxInnovationOther/hide")
    public Result hidelWcxInnovationOther(String wcxInnovationOtherId, HttpServletRequest request) {
        AccessToken accessToken = wcxUserService.getAccessToken(request);
        String sysUserCode = accessToken.getUserCode();
        TbSysUser sysUser = tbSysUserService.findByUserCode(sysUserCode);
        wcxInnovationOtherService.hideReport(wcxInnovationOtherId, sysUser.getUserCode(), sysUser.getUserName());
        return new Result().setResultMsg(WcxConstant.DEAL_SUCCESS);
    }
}
