package com.fs.app.controller;



import com.fs.common.core.page.TableDataInfo;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyUser;
import com.fs.company.service.ICompanyUserService;
import com.fs.his.domain.FsDoctor;
import com.fs.his.domain.FsDoctorPatient;
import com.fs.his.domain.FsPrescribe;
import com.fs.his.param.CollectionPhoneParam;
import com.fs.his.param.PrescribePhoneParam;
import com.fs.his.service.IFsDoctorPatientService;
import com.fs.his.service.IFsDoctorService;
import com.fs.his.service.IFsPrescribeService;
import com.fs.hisStore.service.IFsUserInformationCollectionService;
import com.fs.hisStore.vo.FsUserInformationCollectionOverviewVo;
import com.fs.qw.domain.FsCompanyCustomer;
import com.fs.qw.service.IFsCompanyCustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.fs.common.core.domain.AjaxResult;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 患者信息Controller
 *
 * @author yourname
 * @date 2026-04-28
 */

@Slf4j
@Api(tags = "医生端-患者信息表")
@RestController
@RequestMapping("/app/patientInfo")
public class FsDoctorPatientController extends AppBaseController {
    @Autowired
    private IFsDoctorPatientService fsDoctorPatientService;

    @Autowired
    private IFsDoctorService fsDoctorService;

    @Autowired
    private IFsPrescribeService fsPrescribeService;

    @Autowired
    private IFsUserInformationCollectionService fsUserInformationCollectionService;

    @Autowired
    private IFsCompanyCustomerService fsCompanyCustomerService;

    @Autowired
    private ICompanyUserService companyUserService;

    /**
     * 查询患者信息列表
     */
    @ApiOperation("查询患者列表")
    @GetMapping("/list")
    public AjaxResult list(FsDoctorPatient fsDoctorPatient) {
        String doctorId = getDoctorId();
        if (StringUtils.isBlank(doctorId)){
            return AjaxResult.error("请重新登录");
        }
        fsDoctorPatient.setDoctorId(Long.valueOf(doctorId));
        PageHelper.startPage(fsDoctorPatient.getPageNum(), fsDoctorPatient.getPageSize());
        List<FsDoctorPatient> list = fsDoctorPatientService.selectFsDoctorPatientList(fsDoctorPatient);
        for (FsDoctorPatient item : list) {
            if (item.getPhone() != null) {
                item.setPhone(item.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
        }
        PageInfo<FsDoctorPatient> listPageInfo=new PageInfo<>(list);
        return AjaxResult.success(listPageInfo);
    }

    /**
     * 导出患者信息
     */
    @ApiOperation("导出患者信息")
    @PostMapping("/export")
    public void export(HttpServletResponse response, FsDoctorPatient fsDoctorPatient) {
        List<FsDoctorPatient> list = fsDoctorPatientService.selectFsDoctorPatientList(fsDoctorPatient);
        ExcelUtil<FsDoctorPatient> util = new ExcelUtil<>(FsDoctorPatient.class);
        try {
            util.exportExcel(response, list, "患者信息数据");
        }catch (Exception e){
            log.error("导出患者信息异常",e);
            throw new CustomException("导出患者信息异常");
        }

    }

    /**
     * 获取患者信息详细信息
     */
    @ApiOperation("获取患者详情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        FsDoctorPatient fsDoctorPatient = fsDoctorPatientService.selectFsDoctorPatientById(id);
        if (fsDoctorPatient.getPhone() != null) {
            fsDoctorPatient.setPhone(fsDoctorPatient.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
        }
        return AjaxResult.success(fsDoctorPatient);
    }

    /**
     * 新增患者信息
     */
    @ApiOperation("新增患者")
    @PostMapping
    public AjaxResult add(@RequestBody FsDoctorPatient fsDoctorPatient) {
        return AjaxResult.success(fsDoctorPatientService.insertFsDoctorPatient(fsDoctorPatient));
    }

    /**
     * 修改患者信息
     */
    @ApiOperation("修改患者")
    @PutMapping
    public AjaxResult edit(@RequestBody FsDoctorPatient fsDoctorPatient) {
        return AjaxResult.success(fsDoctorPatientService.updateFsDoctorPatient(fsDoctorPatient));
    }

    /**
     * 删除患者信息
     */
    @ApiOperation("删除患者")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return AjaxResult.success(fsDoctorPatientService.deleteFsDoctorPatientByIds(ids));
    }

    /**
     * 获取当前登录医生信息(姓名、执业证号、签名图片)
     * */
    @ApiOperation("获取当前登录医生信息")
    @GetMapping("/getDoctorBaseInfo")
    public AjaxResult getDoctorInfo(){
        String doctorId = getDoctorId();
        if (StringUtils.isBlank(doctorId)){
            return AjaxResult.error("请重新登录");
        }
        FsDoctor fsDoctor = fsDoctorService.selectFsDoctorByDoctorId(Long.valueOf(doctorId));
        return AjaxResult.success(fsDoctor);
    }

    /**
     * 根据传入手机号获取处方列表信息
     * */
    @PostMapping("/getPrescribeListByPhone")
    public AjaxResult getPrescribeListByPhone(@RequestBody PrescribePhoneParam param){
        PageHelper.startPage(param.getPageNum(),param.getPageSize());
        List<FsPrescribe> list = fsPrescribeService.selectPrescribeListByEncryptPhone(param.getPhone());
        PageInfo<FsPrescribe> listPageInfo=new PageInfo<>(list);
        return AjaxResult.success(listPageInfo);
    }

    /**
     * 根据传入手机号获取信息采集列表数据
     * */
    @PostMapping("/getCollectionInfoListByPhone")
    public AjaxResult getCollectionInfoListByPhone(@RequestBody CollectionPhoneParam param){
        PageHelper.startPage(param.getPageNum(),param.getPageSize());
        List<FsUserInformationCollectionOverviewVo> list = fsUserInformationCollectionService.selectUserInformationCollectionByPhone(param.getPhone());
        PageInfo<FsUserInformationCollectionOverviewVo> listPageInfo=new PageInfo<>(list);
        return AjaxResult.success(listPageInfo);
    }

    /**
     * 医生端-查询可关联销售列表
     */
    @GetMapping("/companyUserList")
    public AjaxResult companyUserList() {
        String doctorId = getDoctorId();
        if (StringUtils.isBlank(doctorId)){
            return AjaxResult.error("请重新登录");
        }
        // 查询关联的销售集合
        List<CompanyUser> companyUserList = companyUserService.selectCompanyUserListByDoctorId(Long.valueOf(doctorId));
        return AjaxResult.success(companyUserList);
    }

    /**
     * 医生端-查询关联销售的所有客户列表
     */
    @GetMapping("/companyCustomerList")
    public AjaxResult companyCustomerList(FsCompanyCustomer fsCompanyCustomer) {
        String doctorId = getDoctorId();
        if (StringUtils.isBlank(doctorId)){
            return AjaxResult.error("请重新登录");
        }
        AjaxResult AjaxResult = new AjaxResult();
        // 查询关联的销售id集合
        List<CompanyUser> companyUserList = companyUserService.selectCompanyUserListByDoctorId(Long.valueOf(doctorId));
        List<Long> companyUserIds = companyUserList.stream().map(CompanyUser::getUserId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(companyUserIds)){
            AjaxResult.put("data", new PageInfo<>(Collections.emptyList()));
        }
        fsCompanyCustomer.setCompanyUserIds(companyUserIds);
        PageHelper.startPage(fsCompanyCustomer.getPageNum(), fsCompanyCustomer.getPageSize());
        List<FsCompanyCustomer> list = fsCompanyCustomerService.selectFsCompanyCustomerListByCompanyUserIds(fsCompanyCustomer);
        //电话号码脱敏
        for (FsCompanyCustomer item : list) {
            if (item.getPhone() != null) {
                item.setPhone(item.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
        }
        AjaxResult.put("data", new PageInfo<>(list));
        return AjaxResult;
    }

    /**
     * 医生端-查询关联销售的客户详情
     */
    @GetMapping("/companyCustomer/{id}")
    public AjaxResult getCompanyCustomerInfo(@PathVariable Long id) {
        FsCompanyCustomer fsCompanyCustomer = fsCompanyCustomerService.selectFsCompanyCustomerById(id);
        if (fsCompanyCustomer.getPhone() != null) {
            fsCompanyCustomer.setPhone(fsCompanyCustomer.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
        }
        return AjaxResult.success(fsCompanyCustomer);
    }
}