package com.fs.company.controller.company;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.his.param.FsDoctorParam;
import com.fs.his.service.IFsDoctorService;
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.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

import static com.fs.his.utils.PhoneUtil.decryptAutoPhoneMk;
import static com.fs.his.utils.PhoneUtil.encryptPhone;

/**
 * 医生管理Controller
 *
 * @author fs
 * @date 2023-06-05
 */
@RestController
@RequestMapping("/his/doctor")
public class FsDoctorController extends BaseController
{
    @Autowired
    private IFsDoctorService fsDoctorService;
    @Autowired
    private RedisCacheUtil redisCacheUtil;
    /**
     * 查询医生管理列表
     */
    @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);
    }

    /**
     * 获取医生管理详细信息
     */
    @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);
    }





    /**
     * 查询医生用户
     */
    @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);
    }


    @GetMapping("/getDocVoList")
    public TableDataInfo getDocVoList(FsDoctorParam param) {
        startPage();
        List<FsDoctorVO> list = fsDoctorService.selectDocVOByNameAndPhone(param);
        if (list == null || list.isEmpty()) {
            param.setMobile(encryptPhone(param.getMobile()));
            list = fsDoctorService.selectDocVOByNameAndPhone(param);
        }

        if (list != null) {
            list.forEach( item -> {
                if (item.getMobile() != null)
                    item.setMobile(decryptAutoPhoneMk(item.getMobile()));
            });
        }
        return getDataTable(list);
    }



}
