package com.fs.company.controller.store;

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.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.framework.security.LoginUser;
import com.fs.framework.security.SecurityUtils;
import com.fs.his.domain.FsHealthRecord;
import com.fs.his.param.FsHealthRecordListParam;
import com.fs.his.service.IFsHealthRecordService;
import com.fs.his.vo.FsHealthRecordListPVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

/**
 * 健康档案Controller
 *
 * @author fs
 * @date 2024-09-02
 */
@RestController
@RequestMapping("/store/healthRecord")
public class FsHealthRecordController extends BaseController
{
    @Autowired
    private IFsHealthRecordService fsHealthRecordService;

    /**
     * 查询健康档案列表
     */
    @PreAuthorize("@ss.hasPermi('store:healthRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsHealthRecordListParam fsHealthRecord)
    {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsHealthRecord.setCompanyId(loginUser.getCompany().getCompanyId());
        if(fsHealthRecord.getPhoneMk()!=null&&fsHealthRecord.getPhone()!=""){
            fsHealthRecord.setPhone(encryptPhone(fsHealthRecord.getPhoneMk()));
        }
        List<FsHealthRecordListPVO> list = fsHealthRecordService.selectFsHealthRecordCompanyListVO(fsHealthRecord);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('store:healthRecord:myList')")
    @GetMapping("/myList")
    public TableDataInfo myList(FsHealthRecordListParam fsHealthRecord)
    {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsHealthRecord.setCompanyId(loginUser.getCompany().getCompanyId());
        fsHealthRecord.setCompanyUserId(loginUser.getUser().getUserId());
        if(fsHealthRecord.getPhoneMk()!=null&&fsHealthRecord.getPhone()!=""){
            fsHealthRecord.setPhone(encryptPhone(fsHealthRecord.getPhoneMk()));
        }
        List<FsHealthRecordListPVO> list = fsHealthRecordService.selectFsHealthRecordCompanyListVO(fsHealthRecord);
        return getDataTable(list);
    }

    /**
     * 导出健康档案列表
     */
    @PreAuthorize("@ss.hasPermi('store:healthRecord:export')")
    @Log(title = "健康档案", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsHealthRecordListParam fsHealthRecord)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsHealthRecord.setCompanyId(loginUser.getCompany().getCompanyId());
        fsHealthRecord.setCompanyUserId(loginUser.getUser().getUserId());
        if(fsHealthRecord.getPhoneMk()!=null&&fsHealthRecord.getPhone()!=""){
            fsHealthRecord.setPhone(encryptPhone(fsHealthRecord.getPhoneMk()));
        }
        List<FsHealthRecordListPVO> list = fsHealthRecordService.selectFsHealthRecordCompanyListVO(fsHealthRecord);
        ExcelUtil<FsHealthRecordListPVO> util = new ExcelUtil<FsHealthRecordListPVO>(FsHealthRecordListPVO.class);
        return util.exportExcel(list, "健康档案数据");
    }

    /**
     * 获取健康档案详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fsHealthRecordService.selectFsHealthRecordById(id));
    }



    /**
     * 修改健康档案
     */
    @PreAuthorize("@ss.hasPermi('his:healthRecord:edit')")
    @Log(title = "健康档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsHealthRecord fsHealthRecord)
    {
        return toAjax(fsHealthRecordService.updateFsHealthRecord(fsHealthRecord));
    }


}
