package com.boman.web.core.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.boman.common.core.utils.SecurityUtils;
import com.boman.common.core.utils.StringUtils;
import com.boman.common.core.utils.poi.ExcelUtil;
import com.boman.common.core.web.controller.BaseController;
import com.boman.domain.TableDataInfo;
import com.boman.common.log.annotation.Log;
import com.boman.common.log.enums.BusinessType;
import com.boman.domain.dto.AjaxResult;
import com.boman.domain.VaccineInfoOperation;
import com.boman.domain.VaccineInfoUser;
import com.boman.web.core.service.vaccineInfo.IVaccineInfoService;
import com.boman.web.core.service.vaccineInfo.IVaccineInfoUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * 疫苗信息Controller
 *
 * @author ruoyi
 * @date 2021-09-05
 */
@RestController
@RequestMapping("/core/info")
public class VaccineInfoController extends BaseController {
    @Autowired
    private IVaccineInfoService vaccineInfoService;

    @Autowired
    private IVaccineInfoUserService vaccineInfoUserService;

    /**
     * 查询疫苗信息列表
     */
//    @PreAuthorize(hasPermi = "@ss.hasPermi('core:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(VaccineInfoOperation vaccineInfo) {
        List<VaccineInfoOperation> list = vaccineInfoService.selectVaccineInfoList(vaccineInfo);
        return getDataTable(list);
    }

    @Log(title = "导出疫苗信息", businessType = BusinessType.EXPORT)
//    @PreAuthorize(hasPermi = "core:info:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, VaccineInfoOperation vaccineInfo) throws IOException  {
        List<VaccineInfoOperation> list = vaccineInfoService.selectVaccineInfoList(vaccineInfo);
        ExcelUtil<VaccineInfoOperation> util = new ExcelUtil<VaccineInfoOperation>(VaccineInfoOperation.class);
        util.exportExcel(response, list, "疫苗信息");
    }

    /**
     * 获取疫苗信息详细信息
     */
//    @PreAuthorize(hasPermi = "@ss.hasPermi('core:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(vaccineInfoService.selectVaccineInfoById(id));
    }

    /**
     * 新增疫苗信息
     */
//    @PreAuthorize(hasPermi = "@ss.hasPermi('core:info:add')")
    @Log(title = "疫苗信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VaccineInfoOperation vaccineInfo) {
        String msg = this.validate(vaccineInfo);
        if(StringUtils.isNotEmpty(msg)) {
            return AjaxResult.error(msg);
        }
        vaccineInfo.setCreateBy(SecurityUtils.getUsername());
        return vaccineInfoService.insertVaccineInfo(vaccineInfo);
    }

    /**
     * 修改疫苗信息
     */
//    @PreAuthorize(hasPermi = "@ss.hasPermi('core:info:edit')")
    @Log(title = "疫苗信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VaccineInfoOperation vaccineInfo) {
      /*  vaccineInfo.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(vaccineInfoService.updateVaccineInfo(vaccineInfo));*/
        String msg = this.validate(vaccineInfo);
        if(StringUtils.isNotEmpty(msg)) {
            return AjaxResult.error(msg);
        }
        vaccineInfo.setUpdateBy(SecurityUtils.getUsername());
        return vaccineInfoService.updateVaccineInfo(vaccineInfo);
    }

    /**
     * 删除疫苗信息
     */
//    @PreAuthorize(hasPermi = "@ss.hasPermi('core:info:remove')")
    @Log(title = "疫苗信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(vaccineInfoService.deleteVaccineInfoByIds(ids));
    }

    /**
     * 删除用户接种疫苗信息
     */
//    @PreAuthorize(hasPermi = "@ss.hasPermi('core:user:remove')")
    @Log(title = "用户接种疫苗信息", businessType = BusinessType.DELETE)
    @GetMapping("/vaccineInfoUser/{ids}")
    public AjaxResult removeVaccineInfoUser(@PathVariable Long[] ids) {
        return toAjax(vaccineInfoUserService.deleteVaccineInfoUserByIds(ids));
    }

    /**
     * 根据身份证号查询用户信息
     */
    @PostMapping("/findHjInfo")
    public AjaxResult findHjInfo(@RequestBody VaccineInfoOperation vaccineInfo) {
        VaccineInfoOperation hjInfo = vaccineInfoService.findHjInfo(vaccineInfo);
        return AjaxResult.success(hjInfo);
    }

    /**
     * 根据身份证号查询用户疫苗信息集合
     */
    @PostMapping("/findInfoUser")
    public AjaxResult findInfoUser(@RequestBody VaccineInfoUser vaccineInfoUser) {
        List<VaccineInfoUser> vaccineInfoUsers = vaccineInfoUserService.selectInfoUserByIdCard(vaccineInfoUser);
        return AjaxResult.success(vaccineInfoUsers);
    }


    /**
     * 根据身份证号查询用户疫苗信息集合
     */
    @PostMapping("/syncDataTest")
    public AjaxResult syncData() throws Exception {
        vaccineInfoUserService.syncData();
        return AjaxResult.success();
    }

    private String validate(VaccineInfoOperation vaccineInfo) {
        String isVaccination = vaccineInfo.getIsVaccination();
        String msg = "";
        if ("否".equals(isVaccination)){
            return msg;
        }
        List<VaccineInfoUser> vaccineInfoUserList = vaccineInfo.getVaccineInfoUserList();
        for (VaccineInfoUser vaccineInfoUser : vaccineInfoUserList) {
            String vaccineName = vaccineInfoUser.getVaccineName();
            Date vaccinationTime =  vaccineInfoUser.getVaccinationTime();
            String vaccinationPlace = vaccineInfoUser.getVaccinationPlace();
            String jici = vaccineInfoUser.getJici();
            if(StringUtils.isEmpty(vaccineName) &&
                    vaccinationTime == null  &&
                    StringUtils.isEmpty(vaccinationPlace) &&
                    StringUtils.isEmpty(jici)) {
                continue;
            }
            if(StringUtils.isEmpty(vaccineName) ||
                    vaccinationTime == null  ||
                    StringUtils.isEmpty(vaccinationPlace) ||
                    StringUtils.isEmpty(jici)) {
                msg = "请确认疫苗接种信息是否填写完整！";
                break;
            }
        }
        return msg;
    }
}
