package com.saye.hospitalgd.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.saye.hospitalgd.commons.date.DateDUtil;
import com.saye.hospitalgd.commons.log.LogUtil;
import com.saye.hospitalgd.scheduler.jobMethod.MedicalInsuranceReconciliationMethod;
import com.saye.hospitalgd.service.MedicalInsuranceReconciliationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author thuang
 * @version 1.0
 * @description: 医保对账Controller
 * @date 2025/10/24
 */
@Api(value = "医保对账相关接口")
@Controller
@RequestMapping("/medicalInsuranceReconciliation")
public class MedicalInsuranceReconciliationController {

    @Autowired
    private MedicalInsuranceReconciliationService medicalInsuranceReconciliationService;

    /**
     * 跳转到医保对账页面
     */
    @RequestMapping("/toMedicalInsuranceReconciliation")
    public String toMedicalInsuranceReconciliation(ModelMap modelMap) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, -1);
        Date startDate = calendar.getTime();

        String startTime = DateDUtil.DateToStr(DateDUtil.yyyy_MM_dd, startDate);
        String endTime = DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd);

        modelMap.addAttribute("startTime", startTime);
        modelMap.addAttribute("endTime", endTime);

        return "financialReconciliation/medicalInsuranceReconciliation";
    }

    /**
     * 跳转到医保对账结果页面
     */
    @RequestMapping("/toMedicalInsuranceReconciliationResult")
    public String toMedicalInsuranceReconciliationResult(ModelMap modelMap) {
        return "financialReconciliation/medicalInsuranceReconciliationResult";
    }

    /**
     * 执行医保对账
     */
    @ApiOperation(value = "执行医保对账", notes = "")
    @PostMapping("/executeMedicalInsuranceReconciliation")
    @ResponseBody
    public HashMap<Object, Object> executeMedicalInsuranceReconciliation(
            @ApiParam(value = "对账日期") @RequestParam String trade_date) {
        
        HashMap<Object, Object> responseMap = new HashMap<>();
        
        try {
            LogUtil.info(this.getClass(), "开始执行医保对账，日期：" + trade_date);
            
            MedicalInsuranceReconciliationMethod reconciliationMethod = new MedicalInsuranceReconciliationMethod();
            HashMap<Object, Object> result = reconciliationMethod.executeMedicalInsuranceReconciliation(trade_date);
            
            responseMap.put("code", "0".equals(result.get("errCode")) ? 0 : 1);
            responseMap.put("msg", result.get("errMsg"));
            responseMap.put("data", result.get("reconciliationResults"));
            
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.error(this.getClass(), "医保对账失败：" + e.getMessage());
            responseMap.put("code", 1);
            responseMap.put("msg", "医保对账失败：" + e.getMessage());
        }
        
        return responseMap;
    }

    /**
     * 查询医保对账结果
     */
    @ApiOperation(value = "查询医保对账结果", notes = "")
    @GetMapping("/findMedicalInsuranceReconciliationResult")
    @ResponseBody
    public HashMap<Object, Object> findMedicalInsuranceReconciliationResult(
            @ApiParam(value = "开始日期") String startDate,
            @ApiParam(value = "结束日期") String endDate,
            @ApiParam(value = "险种类型") String insutype,
            @ApiParam(value = "清算类别") String clrType,
            @ApiParam(value = "对账结果") String stmtRslt,
            @ApiParam(value = "页码") int page,
            @ApiParam(value = "每页数量") int limit) {
        
        HashMap<Object, Object> responseMap = new HashMap<>();
        
        try {
            HashMap<Object, Object> queryMap = new HashMap<>();
            if (startDate != null && !"".equals(startDate)) {
                queryMap.put("startDate", startDate);
            }
            if (endDate != null && !"".equals(endDate)) {
                queryMap.put("endDate", endDate);
            }
            if (insutype != null && !"".equals(insutype)) {
                queryMap.put("insutype", insutype);
            }
            if (clrType != null && !"".equals(clrType)) {
                queryMap.put("clr_type", clrType);
            }
            if (stmtRslt != null && !"".equals(stmtRslt)) {
                queryMap.put("stmt_rslt", stmtRslt);
            }

            PageHelper.startPage(page, limit);
            PageInfo<HashMap<Object, Object>> pageInfo = new PageInfo<>(
                medicalInsuranceReconciliationService.findMedicalInsuranceReconciliationResult(queryMap)
            );

            responseMap.put("code", 0);
            responseMap.put("msg", "OK");
            responseMap.put("count", pageInfo.getTotal());
            responseMap.put("data", pageInfo.getList());
            
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.error(this.getClass(), "查询医保对账结果失败：" + e.getMessage());
            responseMap.put("code", 1);
            responseMap.put("msg", "查询失败：" + e.getMessage());
        }
        
        return responseMap;
    }

    @ApiOperation(value = "更新医保对账备注", notes = "")
    @PostMapping("/updateMedicalInsuranceReconciliationRemark")
    @ResponseBody
    public HashMap<Object, Object> updateMedicalInsuranceReconciliationRemark(
            @ApiParam(value = "记录ID") @RequestParam String id,
            @ApiParam(value = "备注") @RequestParam(required = false) String remark) {

        HashMap<Object, Object> responseMap = new HashMap<>();

        try {
            if (id == null || "".equals(id.trim())) {
                responseMap.put("code", 1);
                responseMap.put("msg", "id不能为空");
                return responseMap;
            }

            HashMap<Object, Object> updateMap = new HashMap<>();
            updateMap.put("id", id.trim());
            updateMap.put("remark", remark == null ? "" : remark.trim());

            medicalInsuranceReconciliationService.updateMedicalInsuranceReconciliationRemark(updateMap);

            responseMap.put("code", 0);
            responseMap.put("msg", "OK");
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.error(this.getClass(), "更新医保对账备注失败：" + e.getMessage());
            responseMap.put("code", 1);
            responseMap.put("msg", "更新失败：" + e.getMessage());
        }

        return responseMap;
    }
}


