/*
 *    Copyright (c) 2018-2025, hnqz All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: hnqz
 */

package com.qunzhixinxi.hnqz.admin.controller;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qunzhixinxi.hnqz.admin.entity.WmScorePackage;
import com.qunzhixinxi.hnqz.admin.entity.WmTask;
import com.qunzhixinxi.hnqz.admin.service.WmScorePackageService;
import com.qunzhixinxi.hnqz.admin.service.WmTaskService;
import com.qunzhixinxi.hnqz.common.core.util.R;
import com.qunzhixinxi.hnqz.common.log.annotation.SysLog;
import com.qunzhixinxi.hnqz.admin.entity.WmReport;
import com.qunzhixinxi.hnqz.admin.service.WmReportService;
import com.qunzhixinxi.hnqz.common.security.service.HnqzUser;
import com.qunzhixinxi.hnqz.common.security.util.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 报告审核表
 *
 * @author pigx code generator
 * @date 2020-12-21 20:57:26
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wmreport" )
@Api(value = "wmreport", tags = "报告审核表管理")
public class WmReportController {

	private final  WmReportService wmReportService;
	private final WmScorePackageService wmScorePackageService;
	private final WmTaskService wmTaskService;
	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param wmReport 报告审核表
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page" )
	@PreAuthorize("@pms.hasPermission('admin_wmreport_view')" )
	public R getWmReportPage(Page page, WmReport wmReport) {
		return R.ok(wmReportService.page(page, Wrappers.query(wmReport)));
	}


	/**
	 * 通过id查询报告审核表
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}" )
	@PreAuthorize("@pms.hasPermission('admin_wmreport_view')" )
	public R getById(@PathVariable("id" ) Integer id) {
		return R.ok(wmReportService.getById(id));
	}

	/**
	 * 新增报告审核表
	 * @param wmReport 报告审核表
	 * @return R
	 */
	@ApiOperation(value = "新增报告审核表", notes = "新增报告审核表")
	@SysLog("新增报告审核表" )
	@PostMapping
	@PreAuthorize("@pms.hasPermission('admin_wmreport_add')" )
	public R save(@RequestBody WmReport wmReport) {
		return R.ok(wmReportService.save(wmReport));
	}

	/**
	 * 修改报告审核表
	 * @param wmReport 报告审核表
	 * @return R
	 */
	@ApiOperation(value = "修改报告审核表", notes = "修改报告审核表")
	@SysLog("修改报告审核表" )
	@PutMapping
	@PreAuthorize("@pms.hasPermission('admin_wmreport_edit')" )
	public R updateById(@RequestBody WmReport wmReport) {
		return R.ok(wmReportService.updateById(wmReport));
	}

	/**
	 * 通过id删除报告审核表
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除报告审核表", notes = "通过id删除报告审核表")
	@SysLog("通过id删除报告审核表" )
	@DeleteMapping("/{id}" )
	public R removeById( @PathVariable("id")Integer id) {
		WmReport wmReport=wmReportService.getById(id);
		if(null==wmReport){
			return R.failed("未查询到报告信息");
		}
		wmReport.setDelFlag("1");
		wmReportService.updateById(wmReport);
		WmScorePackage scorePackage=wmScorePackageService.getById(wmReport.getScoreId());
		//typeid  0 药企  1一级代理商 2二级代理商 3全职学术推广员 4兼职学术推广员
		LambdaUpdateWrapper<WmTask> updateWrapper=new LambdaUpdateWrapper<>();

		if("0".equals(scorePackage.getTypeid())){//药企
			if("1".equals(wmReport.getDelFlag())){
				updateWrapper.set(WmTask::getReportDrugId,null);
				updateWrapper.set(WmTask::getReportDrugApprovalOpinion,null);
				updateWrapper.set(WmTask::getReportDrugApprovalInfo,null);
			}
			updateWrapper.set(WmTask::getReportDrugApprovalStatus,"0");
			updateWrapper.eq(WmTask::getReportDrugId,wmReport.getId());
		}else if("1".equals(scorePackage.getTypeid())){//一级代理商类型
			if("1".equals(wmReport.getDelFlag())){
				updateWrapper.set(WmTask::getReportOneId,null);
				updateWrapper.set(WmTask::getReportOneApprovalOpinion,null);
				updateWrapper.set(WmTask::getReportOneApprovalInfo,null);
			}
			if(SecurityUtils.getRoles().contains(3)){//药企
				updateWrapper.set(WmTask::getReportDrugApprovalStatus,"0");
			}
			updateWrapper.set(WmTask::getReportOneApprovalStatus,"0");
			updateWrapper.eq(WmTask::getReportOneId,wmReport.getId());
		}else if("2".equals(scorePackage.getTypeid())){//二级代理商类型
			if("1".equals(wmReport.getDelFlag())){
				updateWrapper.set(WmTask::getReportSecondId,null);
				updateWrapper.set(WmTask::getReportSecondApprovalOpinion,null);
				updateWrapper.set(WmTask::getReportSecondApprovalInfo,null);
			}
			if(SecurityUtils.getRoles().contains(4)){// 一级CSO
				updateWrapper.set(WmTask::getReportOneApprovalStatus,"0");
			}
			updateWrapper.set(WmTask::getReportSecondApprovalStatus,"0");
			updateWrapper.eq(WmTask::getReportSecondId,wmReport.getId());
		}
		return R.ok(wmTaskService.update(updateWrapper));
	}





	/**
	 * 结算报告管理--列表查询
	 * @param page 分页对象
	 * @param wmReport 报告审核表
	 * @param  type   固定值，1结算报告管理，2结算报告审批
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/reportPage" )
	public R reportPage(Page page, WmReport wmReport,String type,String queryScoreId,String queryName) {
		HnqzUser user=SecurityUtils.getUser();
		wmReport.setDelFlag("0");
		if("1".equals(type)){//1结算报告管理,当前用户是申请结算企业
			wmReport.setDrugEntId(null);
			wmReport.setApplyDrugEntId(user.getDeptId());
		}else{//2结算报告审批,当前用户是结算企业
			wmReport.setApplyDrugEntId(null);
			wmReport.setDrugEntId(user.getDeptId());
		}

		return R.ok(wmReportService.getReportListPage(page,wmReport,queryScoreId,queryName));
	}

	/**
	 * 报告审核状态修改--提交，结单，退回
	 * reportStatus 1:待提交，2:已提交待审批 3：已部分审批  4：已退回 5:已审批待结单 6:已结单
	 * @param wmReport 报告审核表
	 * @return R
	 */
	@ApiOperation(value = "报告审核状态修改--提交或结单", notes = "报告审核状态修改--提交或结单")
	@SysLog("报告审核状态修改--提交或结单" )
	@PostMapping("/updateReportStatus" )
	public R updateReportStatus(@RequestBody WmReport wmReport) {
		WmReport wmReport1=wmReportService.getById(wmReport.getId());
		if(null==wmReport1){
			return R.failed("未查询到报告信息");
		}
		wmReport.setScoreId(wmReport1.getScoreId());
		if(StringUtil.isEmpty(wmReport.getReportStatus())){
			return R.failed("缺少状态参数");
		}
		return wmReportService.updateReportStatus(wmReport);
	}



	/**
	 * 结算报告--审批
	 * reportStatus 1:待提交，2:已提交待审批 3：已部分审批  4：已退回 5:已审批待结单 6:已结单
	 * @param wmReport 报告审核表
	 * @param taskIds 所选任务id
	 * @return R
	 */
	@ApiOperation(value = "结算报告--审批", notes = "结算报告--审批")
	@SysLog("结算报告--审批" )
	@GetMapping("/approvalReport" )
	public R approvalReport(WmReport wmReport, @RequestParam(value = "taskIds") List<String> taskIds,
							String approvalOpinion,String approvalInfo) {
		WmReport wmReport1=wmReportService.getById(wmReport.getId());
		if(null==wmReport1){
			return R.failed("未查询到报告信息");
		}
		if(taskIds.isEmpty()){
			return R.failed("缺少参数:任务id");
		}
		return wmReportService.approvalReport(wmReport1,taskIds,approvalOpinion,approvalInfo);
	}



	/**
	 * 结算报告--查看
	 * @param page 分页对象
	 * @param wmReport 报告审核表
	 * @param isApprovalPage 1审批，0查看
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/getTaskListPage" )
	public R getTaskListPage(Page page, WmReport wmReport, Integer isApprovalPage) {
		WmReport wmReport1=wmReportService.getById(wmReport.getId());
		if(null==wmReport1){
			return R.failed("未查询到报告信息");
		}
		return wmReportService.getTaskListPage(page,wmReport1, isApprovalPage);
	}





}
