package com.diagbot.web;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diagbot.annotation.SysLogger;
import com.diagbot.client.MedAppealExamineInfoServiceClientFacade;
import com.diagbot.dto.GetAppealInfoDTO;
import com.diagbot.dto.GetAppealReviewDTO;
import com.diagbot.dto.GetComplaintRecordDTO;
import com.diagbot.dto.RespDTO;
import com.diagbot.entity.QcresultDetail;
import com.diagbot.facade.MedAppealExamineInfoManagementFacade;
import com.diagbot.facade.SysDictionaryFacade;
import com.diagbot.facade.SysHospitalSetFacade;
import com.diagbot.util.StringUtil;
import com.diagbot.util.SysUserUtils;
import com.diagbot.vo.ApprovedVo;
import com.diagbot.vo.GetAppealInfoVO;
import com.diagbot.vo.GetAppealReviewVO;
import com.diagbot.vo.GetAuditNumberVO;
import com.diagbot.vo.GetComplaintDetailMsgVO;
import com.diagbot.vo.GetComplaintRecordVO;
import com.diagbot.vo.RejectedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;


/**
 * <p>
 * 申诉审核 前端控制器
 * </p>
 *
 * @author wangfeng
 * @since 2020-07-06
 */
@RestController
@RequestMapping("/qc/appealExamineInfo")
@Api(value = "申诉审核API", tags = { "申诉审核API" })
@SuppressWarnings("unchecked")
public class MedAppealExamineInfoController {
    @Autowired
    MedAppealExamineInfoServiceClientFacade medAppealExamineInfoServiceClientFacade;
    @Autowired
    SysDictionaryFacade sysDictionaryFacade;
    @Autowired
    MedAppealExamineInfoManagementFacade medAppealExamineInfoManagementFacade;
    @Autowired
    SysHospitalSetFacade sysHospitalSetFacade;

    @ApiOperation(value = "获取申述记录列表[by:zhanghang]",
            notes = "获取申诉任务接口")
    @PostMapping("/getComplaintRecord")
    @SysLogger("getComplaintRecord")
    public RespDTO<Page<GetComplaintRecordDTO>> getComplaintRecord(@RequestBody GetComplaintRecordVO getComplaintRecordVO) {
        String userId = SysUserUtils.getCurrentPrincipleID();
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        getComplaintRecordVO.setHospitalId(Long.valueOf(hospitalId));
        getComplaintRecordVO.setComplaintId(Long.valueOf(userId));
        return medAppealExamineInfoServiceClientFacade.getComplaintRecord(getComplaintRecordVO);
    }

    @ApiOperation(value = "获取申诉审核列表[by:zhanghang]",
            notes = "获取申诉审核列表")
    @PostMapping("/getAppealReview")
    public RespDTO<Page<GetAppealReviewDTO>> getAppealReview(@RequestBody GetAppealReviewVO getAppealReviewVO) {
        String userId = SysUserUtils.getCurrentPrincipleID();
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        getAppealReviewVO.setHospitalId(Long.valueOf(hospitalId));
        getAppealReviewVO.setAppealId(Long.valueOf(userId));
        return medAppealExamineInfoServiceClientFacade.getAppealReview(getAppealReviewVO);
    }

    @ApiOperation(value = "审核通过[by:zhanghang]",
            notes = "审核通过")
    @PostMapping("/approved")
    @Transactional
    public RespDTO<Boolean> approved(@RequestBody ApprovedVo approvedVo) {
        String userId = SysUserUtils.getCurrentPrincipleID();
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        approvedVo.setHospitalId(Long.valueOf(hospitalId));
        approvedVo.setAppealId(Long.valueOf(userId));

        //获取操作流节点
        String mapping = sysHospitalSetFacade.getValue(Long.parseLong(SysUserUtils.getCurrentHospitalID()),"work_flow_node_mapping");
        if(StringUtil.isNotEmpty(mapping)){
            JSONObject mapJSON = JSONObject.parseObject(mapping);
            approvedVo.setWorkFlowNodeId(mapJSON.getLong("approved"));
        }
        // return RespDTO.onSuc(medAppealExamineInfoServiceClient.approved(approvedVo));
        return RespDTO.onSuc(medAppealExamineInfoManagementFacade.approved(approvedVo));
    }

    @ApiOperation(value = "驳回[by:zhanghang]",
            notes = "驳回")
    @PostMapping("/rejected")
    @Transactional
    public RespDTO<Boolean> rejected(@RequestBody RejectedVo rejectedVo) {
        String userId = SysUserUtils.getCurrentPrincipleID();
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        rejectedVo.setHospitalId(Long.valueOf(hospitalId));
        rejectedVo.setAppealId(Long.valueOf(userId));
        //获取操作流节点
        String mapping = sysHospitalSetFacade.getValue(Long.parseLong(SysUserUtils.getCurrentHospitalID()),"work_flow_node_mapping");
        if(StringUtil.isNotEmpty(mapping)){
            JSONObject mapJSON = JSONObject.parseObject(mapping);
            rejectedVo.setWorkFlowNodeId(mapJSON.getLong("rejected"));
        }
        return medAppealExamineInfoServiceClientFacade.rejected(rejectedVo);
    }
    @ApiOperation(value = "审核查看[by:zhanghang]",
            notes = "审核查看")
    @PostMapping("/getApprovedView")
    public RespDTO<GetAppealInfoDTO> getApprovedView(@RequestBody GetAppealInfoVO getAppealInfoVO) {
        return medAppealExamineInfoServiceClientFacade.getApprovedView(getAppealInfoVO);
    }

    @ApiOperation(value = "获取操作类型及状态[by:zhanghang]",
            notes = "获取操作类型及状态")
    @PostMapping("/getAppealReviewDictionary")
    public RespDTO<Map<String,Map<String,String>>> getAppealReviewDictionary() {
        Map<String, Map<String, String>> appealRejectionDictionary = sysDictionaryFacade.getAppealRejectionDictionary();
        return RespDTO.onSuc(appealRejectionDictionary);
    }

    @ApiOperation(value = "获取待审核数量[by:zhanghang]",
            notes = "获取待审核数量")
    @PostMapping("/getAuditNumber")
    public RespDTO<Integer> getAuditNumber(@RequestBody GetAuditNumberVO getAuditNumberVO) {
        String userId = SysUserUtils.getCurrentPrincipleID();
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        getAuditNumberVO.setHospitalId(Long.valueOf(hospitalId));
        getAuditNumberVO.setId(Long.valueOf(userId));
        return medAppealExamineInfoServiceClientFacade.getAuditNumber(getAuditNumberVO);
    }


    @ApiOperation(value = "申诉记录导出[by:songxl]")
    @PostMapping("/getComplaintRecordExport")
    @SysLogger("getComplaintRecordExport")
    public void getComplaintRecordExport(HttpServletResponse response, @RequestBody @Valid GetComplaintRecordVO getComplaintRecordVO) {
        String userId = SysUserUtils.getCurrentPrincipleID();
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        getComplaintRecordVO.setHospitalId(Long.valueOf(hospitalId));
        getComplaintRecordVO.setComplaintId(Long.valueOf(userId));
        medAppealExamineInfoManagementFacade.getComplaintRecordExport(response, getComplaintRecordVO, medAppealExamineInfoServiceClientFacade);
    }

    @ApiOperation(value = "申诉审核导出[by:songxl]")
    @PostMapping("/getAppealReviewExport")
    @SysLogger("getAppealReviewExport")
    public void getAppealReviewExport(HttpServletResponse response, @RequestBody @Valid GetAppealReviewVO getAppealReviewVO) {
        String userId = SysUserUtils.getCurrentPrincipleID();
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        getAppealReviewVO.setHospitalId(Long.valueOf(hospitalId));
        getAppealReviewVO.setAppealId(Long.valueOf(userId));
        medAppealExamineInfoManagementFacade.getAppealReviewExport(response, getAppealReviewVO, medAppealExamineInfoServiceClientFacade);
    }

    @ApiOperation(value = "获取该申诉记录最新缺陷详情[by:zhanghang]",
            notes = "获取该申诉记录最新缺陷详情")
    @PostMapping("/getComplaintDetailMsg")
    public RespDTO<QcresultDetail> getComplaintDetailMsg(@RequestBody GetComplaintDetailMsgVO getComplaintDetailMsgVO) {
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        getComplaintDetailMsgVO.setHospitalId(Long.valueOf(hospitalId));
        return medAppealExamineInfoServiceClientFacade.getComplaintDetailMsg(getComplaintDetailMsgVO);
    }

}
