package com.fs.app.controller;


import cn.hutool.core.bean.BeanUtil;
import com.fs.app.annotation.Login;
import com.fs.app.exception.FSException;
import com.fs.common.core.domain.R;
import com.fs.common.dto.PersonIdentityDTO;
import com.fs.common.utils.ParseUtils;
import com.fs.store.domain.FsPatient;
import com.fs.store.param.FsPatientAddEditParam;
import com.fs.store.service.IFsPatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.ParseException;
import java.util.Date;
import java.util.List;


@Api("病人接口")
@RestController
@RequestMapping(value="/app/patient")
public class PatientController extends  AppBaseController {

    @Autowired
    private IFsPatientService patientService;


    @Login
    @ApiOperation("获取病人列表")
    @GetMapping("/getPatientList")
    public R getPatientList(){
        try {
            FsPatient map=new FsPatient();
            map.setUserId(Long.parseLong(getUserId()));
            map.setIsDel(0);
            map.setStatus(1);
            List<FsPatient> list=patientService.selectFsPatientList(map);
            return R.ok().put("data",list);
        } catch (Exception e){
            return R.error("操作异常");
        }
    }
    @Login
    @ApiOperation("获取病人详情")
    @GetMapping("/getPatientById")
    public R getPatientById(@RequestParam("patientId")Long patientId){
        FsPatient data=patientService.selectFsPatientById(patientId);
        if(!data.getUserId().equals(Long.parseLong(getUserId()))){

            return R.error("非法操作");
        }
        return R.ok().put("data",data);
    }
    @Login
    @ApiOperation("添加病人")
    @PostMapping("/addPatient")
    public R addPatient(@Valid @RequestBody FsPatientAddEditParam param) throws ParseException {
        // 身份证二要素验证
        PersonIdentityDTO dto = ParseUtils.validateIdCard(param.getPatientName(), param.getIdCard());

        param.setUserId(Long.parseLong(getUserId()));
        FsPatient patient=new FsPatient();
        BeanUtil.copyProperties(param, patient);
        if(dto == null) {
            throw new FSException("身份证二要素验证失败!");
        }
        patient.setBirthday(dto.getBirthdayDate());
        patient.setIsDel(0);
        patient.setStatus(1);
        patient.setCreateTime(new Date());
        patient.setUpdateTime(new Date());
        patientService.insertFsPatient(patient);
        return R.ok("操作成功");
    }

    @Login
    @ApiOperation("编辑病人")
    @PostMapping("/editPatient")
    public R editPatient(@Valid @RequestBody FsPatientAddEditParam param) throws ParseException {
        // 身份证二要素验证
        PersonIdentityDTO dto = ParseUtils.validateIdCard(param.getPatientName(), param.getIdCard());

        FsPatient patient=new FsPatient();
        BeanUtil.copyProperties(param, patient);

        if(dto == null) {
            throw new FSException("身份证二要素验证失败!");
        }
        patient.setBirthday(dto.getBirthdayDate());

        patientService.updateFsPatient(patient);
        return R.ok("操作成功");
    }

    @Login
    @ApiOperation("删除病人")
    @PostMapping("/delPatient")
    public R delPatient(@RequestBody FsPatientAddEditParam param){
        patientService.deleteFsPatientById(param.getPatientId());
        return R.ok("操作成功");
    }


}
