package com.fs.app.controller;

import com.fs.app.annotation.Login;
import com.fs.common.BeanCopyUtils;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.company.domain.CompanyUser;
import com.fs.company.domain.CompanyUserUser;
import com.fs.company.service.ICompanyUserService;
import com.fs.company.service.ICompanyUserUserService;
import com.fs.company.service.impl.CompanyUserServiceImpl;
import com.fs.company.service.impl.CompanyUserUserServiceImpl;
import com.fs.his.domain.FsHealthData;
import com.fs.his.domain.FsHealthLife;
import com.fs.his.domain.FsHealthRecord;
import com.fs.his.param.*;
import com.fs.his.service.IFsHealthDataService;
import com.fs.his.service.IFsHealthHistoryTempService;
import com.fs.his.service.IFsHealthLifeService;
import com.fs.his.service.IFsHealthRecordService;
import com.fs.his.vo.FsHealthDataListUVO;
import com.fs.his.vo.FsHealthHistoryTempListUVO;
import com.fs.his.vo.FsHealthLifeListUVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 健康档案Controller
 *
 * @author fs
 * @date 2024-09-02
 */
@Api("健康档案")
@RestController
@RequestMapping("/app/healthRecord")
public class HealthRecordController extends AppBaseController
{
    @Autowired
    private IFsHealthRecordService fsHealthRecordService;
    @Autowired
    private IFsHealthLifeService fsHealthLifeService;
    @Autowired
    private IFsHealthHistoryTempService fsHealthHistoryTempService;
    @Autowired
    private IFsHealthDataService fsHealthDataService;
    @Autowired
    private ICompanyUserService companyUserService;
    @Autowired
    private ICompanyUserUserService companyUserUserService;
    @Login
    @ApiOperation("获取我的健康档案")
    @GetMapping("/myRecord")
    public AjaxResult myRecord()
    {
        return AjaxResult.success(fsHealthRecordService.selectFsHealthRecordByUserId(Long.parseLong(getUserId())));
    }

    /**
     * 新增健康档案
     */
    @RepeatSubmit
    @Login
    @ApiOperation("新增健康档案")
    @PostMapping("/addRecord")
    public R add(@RequestBody FsHealthRecordAddEditParam param)
    {
        FsHealthRecord user = fsHealthRecordService.selectFsHealthRecordByUserId(Long.parseLong(getUserId()));
        if (user!=null){
            return R.error("已经存在健康档案");
        }
        if (param.getCompanyUserId()!=null&&param.getCompanyUserId()!=0){
            CompanyUserUser map=new CompanyUserUser();
            map.setCompanyUserId(param.getCompanyUserId());
            map.setUserId(Long.parseLong(getUserId()));
            List<CompanyUserUser> list= companyUserUserService.selectCompanyUserUserList(map);
            if(list==null||list.size()==0){
                CompanyUser companyUser=companyUserService.selectCompanyUserById(param.getCompanyUserId());
                if(companyUser!=null&&companyUser.getStatus().equals("0")){
                    map.setCompanyId(companyUser.getCompanyId());
                    companyUserUserService.insertCompanyUserUser(map);
                }
            }
        }

        FsHealthRecord fsHealthRecord = BeanCopyUtils.copy(param, FsHealthRecord.class);
        fsHealthRecord.setUserId(Long.parseLong(getUserId()));
        fsHealthRecordService.insertFsHealthRecord(fsHealthRecord);
        return R.ok();
    }

    /**
     * 修改健康档案
     */
    @Login
    @ApiOperation("修改健康档案")
    @PutMapping("/editRecord")
    public R edit(@RequestBody FsHealthRecordAddEditParam param)
    {
        FsHealthRecord fsHealthRecord = BeanCopyUtils.copy(param, FsHealthRecord.class);
        fsHealthRecordService.updateFsHealthRecord(fsHealthRecord);
        return R.ok();
    }

    /**
     * 查询健康史模板列表
     */
    @Login
    @ApiOperation("查询健康史模板列表")
    @GetMapping("/healthHistoryTempList")
    public R healthHistoryTempList()
    {
        List<FsHealthHistoryTempListUVO> list = fsHealthHistoryTempService.selectFsHealthHistoryTempListUVO();
        return R.ok().put("data",list);
    }

    /**
     * 查询健康生活列表
     */
//    @Login
//    @ApiOperation("查询健康生活列表")
//    @GetMapping("/HealthLifeList")
//    public R HealthLifeList(FsHealthLifeUParam param)
//    {
//        PageHelper.startPage(param.getPageNum(), param.getPageSize());
//        param.setUserId(Long.parseLong(getUserId()));
//        List<FsHealthLifeListUVO> list = fsHealthLifeService.selectFsHealthLifeListUVO(param);
//        PageInfo<FsHealthLifeListUVO> listPageInfo=new PageInfo<>(list);
//        return R.ok().put("data",list);
//    }
    /**
     * 查询今日健康生活
     */
    @Login
    @ApiOperation("查询今日健康生活")
    @GetMapping("/HealthLife")
    public R HealthLife(FsHealthLifeUParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        param.setType(1);
        FsHealthLifeListUVO vo = fsHealthLifeService.selectFsHealthLifeDayByUserId(param);
        param.setType(2);
        FsHealthLifeListUVO vo2 = fsHealthLifeService.selectFsHealthLifeDayByUserId(param);
        param.setType(3);
        FsHealthLifeListUVO vo3 = fsHealthLifeService.selectFsHealthLifeDayByUserId(param);
        Map<String, String> map = new HashedMap<>();

        map.put("eat",vo!=null?vo.getFormJson():null);
        map.put("sleep",vo!=null?vo2.getFormJson():null);
        map.put("sport",vo!=null?vo3.getFormJson():null);
        return R.ok().put("data",map);
    }
    /**
     * 获取健康生活详细信息
     */
    @Login
    @ApiOperation("获取健康生活详细信息")
    @GetMapping(value = "/getHealthLife/{id}")
    public AjaxResult getHealthLife(@PathVariable("id") Long id)
    {
        FsHealthLife life = fsHealthLifeService.selectFsHealthLifeById(id);
        if (life == null) {
            return AjaxResult.error("记录不存在");
        }
        // 仅允许查看本人健康生活记录
        if (life.getUserId() == null || !life.getUserId().equals(Long.parseLong(getUserId()))) {
            return AjaxResult.error("无权查看该记录");
        }
        return AjaxResult.success(life);
    }

    /**
     * 新增健康生活
     */
    @Login
    @ApiOperation("新增健康生活")
    @PostMapping("/addFsHealthLife")
    public R addFsHealthLife(@RequestBody FsHealthLifeAddEditParam param)
    {
        FsHealthLife fsHealthLife = new FsHealthLife();
        fsHealthLife.setUserId(Long.parseLong(getUserId()));
        if (param.getEat()!=null&&param.getEat()!=""){
            fsHealthLife.setType(1);
            fsHealthLife.setFormJson(param.getEat());
            fsHealthLifeService.insertFsHealthLife(fsHealthLife);
        }
        if (param.getSleep()!=null&&param.getSleep()!=""){
            FsHealthLife fsHealthLife2 = new FsHealthLife();
            fsHealthLife2.setUserId(Long.parseLong(getUserId()));
            fsHealthLife2.setType(2);
            fsHealthLife2.setFormJson(param.getSleep());
            fsHealthLifeService.insertFsHealthLife(fsHealthLife2);
        }
        if (param.getSport()!=null&&param.getSport()!=""){
            FsHealthLife fsHealthLife3 = new FsHealthLife();
            fsHealthLife3.setUserId(Long.parseLong(getUserId()));
            fsHealthLife3.setType(3);
            fsHealthLife3.setFormJson(param.getSport());
            fsHealthLifeService.insertFsHealthLife(fsHealthLife3);
        }

        return R.ok();
    }

    @Login
    @ApiOperation("健康数据列表")
    @GetMapping("/HealthDataList")
    public R HealthDataList(FsHealthDataUParam param)
    {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        param.setUserId(Long.parseLong(getUserId()));
        List<FsHealthDataListUVO> list = fsHealthDataService.selectFsHealthDataListUVO(param);
        PageInfo<FsHealthDataListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",list);
    }
    @Login
    @ApiOperation("健康数据详细")
    @GetMapping(value = "/HealthData/{id}")
    public AjaxResult HealthData(@PathVariable("id") Long id)
    {
        FsHealthData data = fsHealthDataService.selectFsHealthDataById(id);
        if (data == null) {
            return AjaxResult.error("记录不存在");
        }
        // 仅允许查看本人健康数据
        if (data.getUserId() == null || !data.getUserId().equals(Long.parseLong(getUserId()))) {
            return AjaxResult.error("无权查看该记录");
        }
        return AjaxResult.success(data);
    }
    @Login
    @ApiOperation("新增健康数据")
    @PostMapping("/addFsHealthData")
    public R add(@RequestBody FsHealthDataAddParam param)
    {
        FsHealthData fsHealthData = BeanCopyUtils.copy(param, FsHealthData.class);
        fsHealthData.setUserId(Long.parseLong(getUserId()));
        fsHealthDataService.insertFsHealthData(fsHealthData);
        return R.ok();
    }
}
