package com.diagbot.web;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.client.MedAppealInfoServiceClientFacade;
import com.diagbot.dto.*;
import com.diagbot.entity.SysUser;
import com.diagbot.entity.SysUserRole;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.facade.SysHospitalSetFacade;
import com.diagbot.facade.SysUserFacade;
import com.diagbot.facade.SysUserRoleFacade;
import com.diagbot.util.StringUtil;
import com.diagbot.util.SysUserUtils;
import com.diagbot.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
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.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 申诉审核 前端控制器
 * </p>
 *
 * @author wangfeng
 * @since 2020-07-06
 */
@RestController
@RequestMapping("/qc/appealInfo")
@Api(value = "申诉审核API", tags = { "申诉审核API" })
@SuppressWarnings("unchecked")
public class MedAppealInfoController {

    @Autowired
    MedAppealInfoServiceClientFacade medAppealInfoServiceClientFacade;

    @Autowired
    SysHospitalSetFacade sysHospitalSetFacade;

    @Autowired
    SysUserRoleFacade sysUserRoleFacade;

    @Autowired
    SysUserFacade sysUserFacade;

    @ApiOperation(value = "申诉[by:songxl]",
            notes = "申诉")
    @PostMapping("/addAppealInfo")
    @Transactional
    public RespDTO<Boolean> addAppealInfo(@RequestBody @Valid AddAppealInfoVO addAppealInfoVO) {
        addAppealInfoVO.setHospitalId(Long.parseLong(SysUserUtils.getCurrentHospitalID()));
        //对内申诉接口如果没有传申诉人，就自己获取
        if(StringUtil.isBlank(addAppealInfoVO.getClaimantId())){
            addAppealInfoVO.setClaimantId(SysUserUtils.getCurrentPrincipleID());
            SysUser user = sysUserFacade.getById(SysUserUtils.getCurrentPrincipleID());
            if(user!=null){
                addAppealInfoVO.setClaimantName(user.getLinkman());
            }
        }
        //获取操作流节点
        String mapping = sysHospitalSetFacade.getValue(Long.parseLong(SysUserUtils.getCurrentHospitalID()), "work_flow_node_mapping");
        //获取是否是单人操作开关
        String single_appeal_falg = sysHospitalSetFacade.getValue(Long.parseLong(SysUserUtils.getCurrentHospitalID()), "single_appeal_falg");
        if(StringUtil.isNotEmpty(single_appeal_falg)) {
            addAppealInfoVO.setSingleAppealFalg(Boolean.parseBoolean(single_appeal_falg));
        }
        if (StringUtil.isNotEmpty(mapping)) {
            JSONObject mapJSON = JSONObject.parseObject(mapping);
            addAppealInfoVO.setWorkFlowNodeId(mapJSON.getLong("addAppealInfo"));
        }
        return medAppealInfoServiceClientFacade.addAppealInfo(addAppealInfoVO);
    }

    @ApiOperation(value = "申诉-对外api接口[by:songxl]",
            notes = "申诉")
    @PostMapping("/addAppealInfoApi")
    @Transactional
    public RespDTO<Boolean> addAppealInfoApi(@RequestBody @Valid AddAppealInfoApiVO addAppealInfoApiVO) {
        AddAppealInfoVO addAppealInfoVO = new AddAppealInfoVO();
        BeanUtils.copyProperties(addAppealInfoApiVO, addAppealInfoVO);
        //获取操作流节点
        String mapping = sysHospitalSetFacade.getValue(addAppealInfoApiVO.getHospitalId(), "work_flow_node_mapping");
        if (StringUtil.isNotEmpty(mapping)) {
            JSONObject mapJSON = JSONObject.parseObject(mapping);
            addAppealInfoVO.setWorkFlowNodeId(mapJSON.getLong("addAppealInfo"));
        }
        //通过医生id获取医生的用户id
//        Long userId = sysUserFacade.getUserId(addAppealInfoApiVO.getClaimantId(), addAppealInfoApiVO.getHospitalId());
//        if (userId != null) {
//            addAppealInfoVO.setClaimantId(userId+"");
//        }
        //获取是否是单人操作开关
        String single_appeal_falg = sysHospitalSetFacade.getValue(addAppealInfoApiVO.getHospitalId(), "single_appeal_falg");
        if(StringUtil.isNotEmpty(single_appeal_falg)) {
            addAppealInfoVO.setSingleAppealFalg(Boolean.parseBoolean(single_appeal_falg));
        }

        return medAppealInfoServiceClientFacade.addAppealInfo(addAppealInfoVO);
    }

    @ApiOperation(value = "获取审核人-对外api接口[by:songxl]",
            notes = "获取审核人")
    @PostMapping("/getReviewerApi")
    public RespDTO<List<GetReviewerDTO>> getReviewerApi(@RequestBody GetReviewerApiVO getReviewerApiVO) {
        //申诉人是否包含自身开关
        String flag = sysHospitalSetFacade.getValue(getReviewerApiVO.getHospitalId(), "self_flag");
        GetReviewerVO getReviewerVO = new GetReviewerVO();
        BeanUtils.copyProperties(getReviewerApiVO, getReviewerVO);
        if (StringUtils.isNotEmpty(flag)) {
            getReviewerVO.setSelfFlag(Boolean.parseBoolean(flag));
        }
        //通过医生id获取医生的用户id
        Long userId = sysUserFacade.getUserId(getReviewerApiVO.getClaimantId(), getReviewerApiVO.getHospitalId());
        if (userId != null) {
            getReviewerVO.setClaimantId(userId+"");
        }
        return medAppealInfoServiceClientFacade.getReviewer(getReviewerVO);
    }

    @ApiOperation(value = "获取审核人[by:songxl]",
            notes = "获取审核人")
    @PostMapping("/getReviewer")
    public RespDTO<List<GetReviewerDTO>> getReviewer(@RequestBody GetReviewerVO getReviewerVO) {
        getReviewerVO.setClaimantId(SysUserUtils.getCurrentPrincipleID());
        getReviewerVO.setHospitalId(Long.parseLong(SysUserUtils.getCurrentHospitalID()));
        //申诉人是否包含自身开关
        String flag = sysHospitalSetFacade.getValue(Long.parseLong(SysUserUtils.getCurrentHospitalID()), "self_flag");
        if (StringUtils.isNotEmpty(flag)) {
            getReviewerVO.setSelfFlag(Boolean.parseBoolean(flag));
        }
        return medAppealInfoServiceClientFacade.getReviewer(getReviewerVO);
    }

    @ApiOperation(value = "再次申诉获取操作类型[by:songxl]",
            notes = "再次申诉获取操作类型")
    @PostMapping("/getAppealOperationType")
    public RespDTO<Map<String, String>> getAppealOperationType(@RequestBody @Valid GetAppealOperationTypeVO getAppealOperationTypeVO) {
        return medAppealInfoServiceClientFacade.getAppealOperationType(getAppealOperationTypeVO);
    }

    @ApiOperation(value = "撤回申诉[by:songxl]",
            notes = "撤回申诉")
    @PostMapping("/cancelAppealInfoApi")
    @Transactional
    public RespDTO<Boolean> cancelAppealInfoApi(@RequestBody @Valid CancelAppealInfoApiVO cancelAppealInfoApiVO) {
        //获取操作流节点
        String mapping = sysHospitalSetFacade.getValue(cancelAppealInfoApiVO.getHospitalId(), "work_flow_node_mapping");
        CancelAppealInfoVO cancelAppealInfoVO = new CancelAppealInfoVO();
        BeanUtils.copyProperties(cancelAppealInfoApiVO, cancelAppealInfoVO);
        if (StringUtil.isNotEmpty(mapping)) {
            JSONObject mapJSON = JSONObject.parseObject(mapping);
            cancelAppealInfoVO.setWorkFlowNodeId(mapJSON.getLong("cancelAppealInfo"));
        }
        //通过医生id获取医生的用户id
        Long userId = sysUserFacade.getUserId(cancelAppealInfoApiVO.getClaimantId(), cancelAppealInfoApiVO.getHospitalId());
        if (userId != null) {
            cancelAppealInfoVO.setClaimantId(userId+"");
        }
        return medAppealInfoServiceClientFacade.cancelAppealInfo(cancelAppealInfoVO);
    }

    @ApiOperation(value = "撤回申诉[by:songxl]",
            notes = "撤回申诉")
    @PostMapping("/cancelAppealInfo")
    @Transactional
    public RespDTO<Boolean> cancelAppealInfo(@RequestBody @Valid CancelAppealInfoVO cancelAppealInfoVO) {
        //获取操作流节点
        String mapping = sysHospitalSetFacade.getValue(Long.parseLong(SysUserUtils.getCurrentHospitalID()), "work_flow_node_mapping");
        if (StringUtil.isNotEmpty(mapping)) {
            JSONObject mapJSON = JSONObject.parseObject(mapping);
            cancelAppealInfoVO.setWorkFlowNodeId(mapJSON.getLong("cancelAppealInfo"));
        }
        cancelAppealInfoVO.setClaimantId(SysUserUtils.getCurrentPrincipleID());
        return medAppealInfoServiceClientFacade.cancelAppealInfo(cancelAppealInfoVO);
    }

    @ApiOperation(value = "申诉记录详情-查看[by:songxl]",
            notes = "申诉记录详情-查看")
    @PostMapping("/getAppealInfo")
    public RespDTO<GetAppealInfoDTO> getAppealInfo(@RequestBody GetAppealInfoVO getAppealInfoVO) {
        return medAppealInfoServiceClientFacade.getAppealInfo(getAppealInfoVO);
    }

    @ApiOperation(value = "申诉审核科室下拉列表查询[by:songxl]",
            notes = "申诉审核科室下拉列表查询")
    @PostMapping("/getAppealDept")
    public RespDTO<List<GetAppealDeptDTO>> getAppealDept(@RequestBody @Valid GetAppealDeptVo getAppealDeptVo) {
        String currentPrincipleID = SysUserUtils.getCurrentPrincipleID();
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        getAppealDeptVo.setHospitalId(Long.valueOf(hospitalId));
        if (Long.valueOf(getAppealDeptVo.getPageType()) == 1) {
            //申诉人
            getAppealDeptVo.setClaimantId(Long.valueOf(currentPrincipleID));
        } else {
            //审核人
            getAppealDeptVo.setCheckId(Long.valueOf(currentPrincipleID));
        }
        return medAppealInfoServiceClientFacade.getAppealDept(getAppealDeptVo);
    }

    @ApiOperation(value = "申诉审核模块下拉列表查询[by:songxl]",
            notes = "申诉审核模块下拉列表查询")
    @PostMapping("/getAppealMode")
    public RespDTO<List<GetAppealModeDTO>> getAppealMode(@RequestBody @Valid GetAppealModeVo getAppealModeVo) {
        String currentPrincipleID = SysUserUtils.getCurrentPrincipleID();
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        getAppealModeVo.setHospitalId(Long.valueOf(hospitalId));
        if (Long.valueOf(getAppealModeVo.getPageType()) == 1) {
            //申诉人
            getAppealModeVo.setClaimantId(Long.valueOf(currentPrincipleID));
        } else {
            //审核人
            getAppealModeVo.setCheckId(Long.valueOf(currentPrincipleID));
        }
        return medAppealInfoServiceClientFacade.getAppealMode(getAppealModeVo);
    }

    @ApiOperation(value = "查询该角色的所有角色id[by:songxl]",
            notes = "查询该角色的所有角色id")
    @PostMapping("/getUserRole")
    public RespDTO<List<Long>> getUserRole() {
        List<Long> roleId = new ArrayList<>();
        List<SysUserRole> list = sysUserRoleFacade.list(new QueryWrapper<SysUserRole>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("user_id", SysUserUtils.getCurrentPrincipleID()));
        for (SysUserRole sysUserRole : list) {
            roleId.add(sysUserRole.getRoleId());
        }
        return RespDTO.onSuc(roleId);
    }
}
