package com.saye.hospitalgd.controller.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.saye.hospitalgd.commons.date.DateDUtil;
import com.saye.hospitalgd.commons.string.StringDUtil;
import com.saye.hospitalgd.commons.uuid.UUIDGenerator;
import com.saye.hospitalgd.model.FinanceUser;
import com.saye.hospitalgd.service.NotifyService;
import com.saye.hospitalgd.service.system.FinanceUserService;
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.HashMap;
import java.util.List;

/**
 * @author thuang
 * @version 1.0
 * @description: 财务人员管理控制器
 * @date 2024/12/19 18:00
 */
@Api(tags = "财务人员管理")
@Controller
@RequestMapping("/financeUser")
public class FinanceUserController {

    @Autowired
    private FinanceUserService financeUserService;

    @Autowired
    private NotifyService notifyService;

    /**
     * @description: 到财务人员管理页面
     * @author thuang
     * @date 2024/12/19 18:00
     * @version 1.0
     */
    @RequestMapping("/toFinanceUser")
    public String toFinanceUser(ModelMap modelMap) {
        return "system/financeUser";
    }

    /**
     * @description: 到财务人员管理测试页面
     * @author thuang
     * @date 2024/12/19 18:30
     * @version 1.0
     */
    @RequestMapping("/toFinanceUserTest")
    public String toFinanceUserTest(ModelMap modelMap) {
        return "system/financeUser_test";
    }

    /**
     * @description: 查询财务人员列表
     * @author thuang
     * @date 2024/12/19 18:00
     * @version 1.0
     */
    @RequestMapping("/findFinanceUserPageList")
    @ResponseBody
    @ApiOperation("查询财务人员列表")
    public HashMap<Object, Object> findFinanceUserPageList(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "wechatName", required = false) String wechatName,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "isActive", required = false) String isActive,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "limit", defaultValue = "20") int limit) {

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

        try {
            HashMap<Object, Object> map = new HashMap<Object, Object>();
            map.put("name", name);
            map.put("wechatName", wechatName);
            map.put("phone", phone);
            map.put("isActive", isActive);

            PageHelper.startPage(page, limit);
            PageInfo<FinanceUser> pageInfo = new PageInfo<FinanceUser>(financeUserService.findFinanceUserPageList(map));

            responseMap.put("code", 0);
            responseMap.put("msg", "OK");
            responseMap.put("count", pageInfo.getTotal());
            responseMap.put("data", pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            responseMap.put("code", 1);
            responseMap.put("msg", "查询失败,原因：" + msg);
        }
        return responseMap;
    }

    /**
     * @description: 添加财务人员
     * @author thuang
     * @date 2024/12/19 18:00
     * @version 1.0
     */
    @RequestMapping("/addFinanceUser")
    @ResponseBody
    @ApiOperation("添加财务人员")
    public HashMap<Object, Object> addFinanceUser(@RequestBody FinanceUser financeUser) {

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

        try {
            financeUser.setId(UUIDGenerator.getUUID());
            financeUser.setCreateTime(DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
            financeUser.setModifyTime(DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
            if (StringDUtil.changeNullToEmpty(financeUser.getIsActive()).equals("")) {
                financeUser.setIsActive("1");
            }

            // 如果有手机号，尝试通过手机号获取OpenID
            if (financeUser.getPhone() != null && !financeUser.getPhone().trim().isEmpty()) {
                try {
                    String openId = notifyService.getUserOpenIdByPhone(financeUser.getPhone());
                    financeUser.setOpenId(openId);
                } catch (Exception e) {
                    // 获取OpenID失败不影响添加财务人员，只记录日志
                    System.err.println("根据手机号获取OpenID失败: " + e.getMessage());
                }
            }

            financeUserService.addFinanceUser(financeUser);

            responseMap.put("code", 0);
            responseMap.put("msg", "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            responseMap.put("code", 1);
            responseMap.put("msg", "添加失败,原因：" + msg);
        }
        return responseMap;
    }

    /**
     * @description: 修改财务人员
     * @author thuang
     * @date 2024/12/19 18:00
     * @version 1.0
     */
    @RequestMapping("/updateFinanceUser")
    @ResponseBody
    @ApiOperation("修改财务人员")
    public HashMap<Object, Object> updateFinanceUser(@RequestBody FinanceUser financeUser) {

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

        try {
            financeUser.setModifyTime(DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));

            // 如果有手机号，尝试通过手机号获取OpenID
            if (financeUser.getPhone() != null && !financeUser.getPhone().trim().isEmpty()) {
                try {
                    String openId = notifyService.getUserOpenIdByPhone(financeUser.getPhone());
                    financeUser.setOpenId(openId);
                } catch (Exception e) {
                    // 获取OpenID失败不影响修改财务人员，只记录日志
                    System.err.println("根据手机号获取OpenID失败: " + e.getMessage());
                }
            }

            financeUserService.updateFinanceUser(financeUser);

            responseMap.put("code", 0);
            responseMap.put("msg", "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            responseMap.put("code", 1);
            responseMap.put("msg", "修改失败,原因：" + msg);
        }
        return responseMap;
    }

    /**
     * @description: 删除财务人员
     * @author thuang
     * @date 2024/12/19 18:00
     * @version 1.0
     */
    @RequestMapping("/deleteFinanceUser")
    @ResponseBody
    @ApiOperation("删除财务人员")
    public HashMap<Object, Object> deleteFinanceUser(@RequestParam("id") @ApiParam("财务人员ID") String id) {

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

        try {
            financeUserService.deleteFinanceUser(id);

            responseMap.put("code", 0);
            responseMap.put("msg", "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            responseMap.put("code", 1);
            responseMap.put("msg", "删除失败,原因：" + msg);
        }
        return responseMap;
    }

    /**
     * @description: 根据ID查询财务人员
     * @author thuang
     * @date 2024/12/19 18:00
     * @version 1.0
     */
    @RequestMapping("/findFinanceUserById")
    @ResponseBody
    @ApiOperation("根据ID查询财务人员")
    public HashMap<Object, Object> findFinanceUserById(@RequestParam("id") @ApiParam("财务人员ID") String id) {

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

        try {
            FinanceUser financeUser = financeUserService.findFinanceUserById(id);

            responseMap.put("code", 0);
            responseMap.put("msg", "查询成功");
            responseMap.put("data", financeUser);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            responseMap.put("code", 1);
            responseMap.put("msg", "查询失败,原因：" + msg);
        }
        return responseMap;
    }

    /**
     * @description: 获取所有启用的财务人员
     * @author thuang
     * @date 2024/12/19 18:00
     * @version 1.0
     */
    @RequestMapping("/findActiveFinanceUsers")
    @ResponseBody
    @ApiOperation("获取所有启用的财务人员")
    public HashMap<Object, Object> findActiveFinanceUsers() {

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

        try {
            List<FinanceUser> financeUsers = financeUserService.findActiveFinanceUsers();

            responseMap.put("code", 0);
            responseMap.put("msg", "查询成功");
            responseMap.put("data", financeUsers);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            responseMap.put("code", 1);
            responseMap.put("msg", "查询失败,原因：" + msg);
        }
        return responseMap;
    }
}
