package com.example.his.api.front.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.IdcardUtil;
import com.example.his.api.common.result.R;
import com.example.his.api.config.sa_token.StpCustomerUtil;
import com.example.his.api.exception.HisException;
import com.example.his.api.front.service.AppointmentService;
import com.example.his.api.front.service.OrderService;
import com.example.his.api.req.FrontAppointment.InsertForm;
import com.example.his.api.req.FrontAppointment.SearchForm;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController("FrontAppointmentController")
@RequestMapping("/front/appointment")
public class AppointmentController {
    @Resource
    private OrderService orderService;

    @Resource
    private AppointmentService appointmentService;

    /**
     * 体检预约-创建
     * @param form
     * @return
     */
    @PostMapping("/insertFrontAppointment")
    @SaCheckLogin(type = StpCustomerUtil.TYPE)
    public R insertFrontAppointment(@RequestBody @Valid InsertForm form) {
        int customerId = StpCustomerUtil.getLoginIdAsInt();
        HashMap param = new HashMap() {{
            put("customerId", customerId);
            put("id", form.getOrderId());
        }};
        boolean bool = orderService.hasOwnOrder(param);
        if (!bool) {
            throw new HisException("预约失败，该订单与您无关");
        }

        String pid = form.getPid();
        //验证身份证是否有效
        if (!IdcardUtil.isValidCard18(pid)) {
            throw new HisException("身份证号码无效");
        }
        String birthday = IdcardUtil.getBirthDate(pid).toDateStr();
        String sex = IdcardUtil.getGenderByIdCard(pid) == 1 ? "男" : "女";

        //验证日期是否为未来60天以内
        DateTime date = DateUtil.parse(form.getDate());
        DateTime tomorrow = DateUtil.tomorrow(); //当前时刻的24小时之后
        DateTime startDate = DateUtil.parse(tomorrow.toDateStr()); //明天凌晨
        DateTime endDate = tomorrow.offset(DateField.DAY_OF_MONTH, 60);
        boolean temp = date.isIn(startDate, endDate);
        if (!temp) {
            throw new HisException("预约日期错误");
        }
        Map map = BeanUtil.beanToMap(form);
        map.put("uuid",IdUtil.simpleUUID().toUpperCase());
        map.put("birthday", birthday);
        map.put("sex",sex);

        String result = appointmentService.insertFrontAppointment(map);
        return R.success(result);
    }

    /**
     * 体检预约-front分页查询
     * @param form
     * @return
     */
    @PostMapping("/searchFrontAppointmentByPage")
    @SaCheckLogin(type = StpCustomerUtil.TYPE)
    public R searchFrontAppointmentByPage(@RequestBody @Valid SearchForm form) {
        int customerId = StpCustomerUtil.getLoginIdAsInt();
        Map param = BeanUtil.beanToMap(form);
        param.put("customerId",customerId);
        PageInfo<HashMap> list = appointmentService.searchFrontAppointmentByPage(param);
        return R.success(list);
    }
}

