package com.fs.his.controller;

import java.util.Base64;
import java.util.List;

import com.fs.common.utils.DateUtils;
import com.fs.common.utils.SecurityUtils;
import com.fs.common.utils.sign.Md5Utils;
import com.fs.his.param.*;
import com.fs.his.utils.RedisCacheUtil;
import com.fs.his.vo.FsDoctorListVO;
import com.fs.his.vo.FsDoctorVO;
import com.fs.his.vo.OptionsVO;
import com.fs.his.vo.UserVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.his.domain.FsDoctor;
import com.fs.his.service.IFsDoctorService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 医生管理Controller
 *
 * @author fs
 * @date 2023-06-05
 */
@RestController
@RequestMapping("/his/doctor")
public class FsDoctorController extends BaseController
{
    @Autowired
    private IFsDoctorService fsDoctorService;
    @Autowired
    private RedisCacheUtil redisCacheUtil;
    /**
     * 查询医生管理列表
     */
    @PreAuthorize("@ss.hasPermi('his:doctor:list') or @ss.hasPermi('his:pharmacist:list') ")
    @GetMapping("/list")
    public TableDataInfo list(FsDoctorParam fsDoctor)
    {
        startPage();
        List<FsDoctorListVO> list = fsDoctorService.selectFsDoctorListVO(fsDoctor);
        for (FsDoctorListVO fsDoctorListVO :list){
            if (fsDoctorListVO.getMobile()!=null){
                fsDoctorListVO.setMobile(fsDoctorListVO.getMobile().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
            if (fsDoctorListVO.getIdCard()!=null){
                fsDoctorListVO.setIdCard(fsDoctorListVO.getIdCard().replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1**********$2"));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出医生管理列表
     */
    @PreAuthorize("@ss.hasPermi('his:doctor:export') or @ss.hasPermi('his:pharmacist:export')")
    @Log(title = "医生管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsDoctorParam fsDoctor)
    {


        List<FsDoctorListVO> list = fsDoctorService.selectFsDoctorListVO(fsDoctor);
        for (FsDoctorListVO fsDoctorListVO :list){
            if (fsDoctorListVO.getIdCard()!=null){
                fsDoctorListVO.setIdCard(fsDoctorListVO.getIdCard().replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1**********$2"));
            }
            if (fsDoctorListVO.getMobile()!=null){
                fsDoctorListVO.setMobile(fsDoctorListVO.getMobile().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
        }
        ExcelUtil<FsDoctorListVO> util = new ExcelUtil<FsDoctorListVO>(FsDoctorListVO.class);
        return util.exportExcel(list, "医生管理数据");
    }

    /**
     * 获取医生管理详细信息
     */
    @GetMapping(value = "/{doctorId}")
    public AjaxResult getInfo(@PathVariable("doctorId") Long doctorId)
    {
        FsDoctorVO fsDoctorVO = fsDoctorService.selectFsDoctorVOByDoctorId(doctorId);

        if (fsDoctorVO.getMobile()!=null){
            fsDoctorVO.setMobile(fsDoctorVO.getMobile().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
        }
        if (fsDoctorVO.getIdCard()!=null){
            fsDoctorVO.setIdCard(fsDoctorVO.getIdCard().replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1**********$2"));
        }
        return AjaxResult.success(fsDoctorVO);
    }

    /**
     * 新增医生
     */
    @PreAuthorize("@ss.hasPermi('his:doctor:add') or @ss.hasPermi('his:pharmacist:add') ")
    @Log(title = "医生管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsDoctor fsDoctor)
    {

        fsDoctor.setPassword(Md5Utils.hash(fsDoctor.getPassword()));
        fsDoctor.setCreateBy(SecurityUtils.getUsername());
        fsDoctor.setIsAudit(0);
        fsDoctor.setPriceJson("[{\"price\":1.00,\"type\":1},{\"price\":1.00,\"type\":2}]");
        fsDoctorService.insertFsDoctor(fsDoctor);

        redisCacheUtil.delRedisKey("getDoctorList");
        redisCacheUtil.delRedisKey("getDoctorDetails");
        return toAjax(1);
    }

    @PreAuthorize("@ss.hasPermi('his:doctor:editFollow') or @ss.hasPermi('his:pharmacist:editFollow') ")
    @Log(title = "随访继承", businessType = BusinessType.UPDATE)
    @PutMapping("/editFollow")
    public AjaxResult editFollow(@RequestBody FsUpdateFollowParam fsDoctor)
    {

        return toAjax(fsDoctorService.editFollowByFsDoctor(fsDoctor));
    }


    /**
     * 修改医生管理
     */
    @PreAuthorize("@ss.hasPermi('his:doctor:edit') or @ss.hasPermi('his:pharmacist:edit') ")
    @Log(title = "医生管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsDoctor fsDoctor)
    {
        if(fsDoctor.getDoctorType()==1){
            if (fsDoctor.getIsPrescribeDoctor()==1){
                fsDoctor.setPrescribeDoctorId(fsDoctor.getDoctorId());
            }
        }
        if (fsDoctor.getMobile()!=null){
            if (fsDoctor.getMobile().contains("*")){
                fsDoctor.setMobile(null);
            }
        }
        if (fsDoctor.getIdCard()!=null){
            if (fsDoctor.getIdCard().contains("*")){
                fsDoctor.setIdCard(null);
            }
        }
        fsDoctorService.updateFsDoctor(fsDoctor);
        redisCacheUtil.delRedisKey("getDoctorList");
        redisCacheUtil.delRedisKey("getDoctorDetails");
        return toAjax(1);
    }

/**
 * 修改医生管理
 */

@PreAuthorize("@ss.hasPermi('his:doctor:inquiry') or @ss.hasPermi('his:pharmacist:inquiry')")
    @Log(title = "医生价格配置", businessType = BusinessType.UPDATE)
    @PutMapping("/editDoctorPrice")
    public AjaxResult edit(@RequestBody FsDoctorPriceParam fsDoctor)
    {
        FsDoctor doc = new FsDoctor();
        doc.setDoctorId(fsDoctor.getDoctorId());
        doc.setPriceJson(fsDoctor.getPriceJson());
        doc.setWorkStatus(fsDoctor.getWorkStatus());
        fsDoctorService.updateFsDoctor(doc);
        redisCacheUtil.delRedisKey("getDoctorList");
        redisCacheUtil.delRedisKey("getDoctorDetails");
        return toAjax(1);
    }

    /**
     * 医生审核
     */
    @PreAuthorize("@ss.hasPermi('his:doctor:audit') or @ss.hasPermi('his:pharmacist:audit')")
    @Log(title = "医生管理", businessType = BusinessType.UPDATE)
    @PutMapping("/editDoctor")
    public AjaxResult editDoctor(@RequestBody FsDoctorEditParam fsDoctor)
    {
        FsDoctor doc = new FsDoctor();
        doc.setDoctorId(fsDoctor.getDoctorId());
        doc.setIsAudit(fsDoctor.getIsAudit());
        doc.setAuditTime(DateUtils.getNowDate());
        if (fsDoctor.getRemark()!=null&&fsDoctor.getRemark()!=""){
            doc.setRemark(fsDoctor.getRemark());
        }
        return toAjax(fsDoctorService.updateFsDoctor(doc));
    }
    /**
     * 删除医生管理
     */
    @PreAuthorize("@ss.hasPermi('his:doctor:remove') or @ss.hasPermi('his:pharmacist:remove')")
    @Log(title = "医生管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{doctorIds}")
    public AjaxResult remove(@PathVariable Long[] doctorIds)
    {
        fsDoctorService.deleteFsDoctorByDoctorIds(doctorIds);
        redisCacheUtil.delRedisKey("getDoctorList");
        redisCacheUtil.delRedisKey("getDoctorDetails");
        return toAjax(1);
    }

    /**
     * 查询医生用户
     */
    @GetMapping("user/list")
    public TableDataInfo userlist(String name)
    {

        List<UserVo> list = fsDoctorService.selectUserVOList(name);
        return getDataTable(list);
    }

    /**
     * 查询医生名称列表
     */
    @GetMapping("userdoc/list")
    public TableDataInfo userDoctorList(String name)
    {
        List<UserVo> list = fsDoctorService.selectUserDocVoList(name);
        return getDataTable(list);
    }
    @GetMapping("doc/list")
    public TableDataInfo docList()
    {
        List<UserVo> list = fsDoctorService.selectAllDocVoList();
        return getDataTable(list);
    }
    @GetMapping("/followDoctorList")
    public TableDataInfo followDoctorList()
    {
        List<OptionsVO> list = fsDoctorService.selectfollowDoctorList();
        return getDataTable(list);
    }
    @GetMapping("/allFollowDoctorList")
    public TableDataInfo allFfollowDoctorList()
    {
        List<OptionsVO> list = fsDoctorService.selectAllfollowDoctorList();
        return getDataTable(list);
    }
    @GetMapping("/getWxaCodeUnLimit")
    public AjaxResult getwxacodeunlimit()
    {

        byte[] bytes = fsDoctorService.getWxaCodeUnLimit();
        String base64 = Base64.getEncoder().encodeToString(bytes);
        return AjaxResult.success("成功",base64);
    }


    /**
     * 重置密码
     */
    @PreAuthorize("@ss.hasPermi('his:doctor:resetpwd') or @ss.hasPermi('his:pharmacist:resetpwd') ")
    @Log(title = "医生管理", businessType = BusinessType.UPDATE)
    @PutMapping("/editPassWord")
    public AjaxResult editPassWord(@RequestBody FsDoctorPwParam fsDoctor)
    {
        FsDoctor doc = new FsDoctor();
        doc.setDoctorId(fsDoctor.getDoctorId());
        doc.setPassword(Md5Utils.hash(fsDoctor.getPassword()));
        return toAjax(fsDoctorService.updateFsDoctor(doc));
    }

}
