package com.yida.data.school.epidemic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.epidemic.EpidemicAccessFlow;
import com.yida.data.common.core.entity.epidemic.EpidemicBaseData;
import com.yida.data.common.core.entity.epidemic.EpidemicKnowledgeData;
import com.yida.data.common.core.entity.epidemic.EpidemicRiskAreasData;
import com.yida.data.school.epidemic.service.EpidemicAccessFlowService;
import com.yida.data.school.epidemic.service.EpidemicBaseDataService;
import com.yida.data.school.epidemic.service.EpidemicKnowledgeDataService;
import com.yida.data.school.epidemic.service.EpidemicRiskAreasDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ccl
 */
@Api(tags = "疫情大数据")
@Slf4j
@RestController
@RequestMapping("/in/epidemic")
@RequiredArgsConstructor
public class InEpidemicController {
    private final EpidemicBaseDataService baseDataService;
    private final EpidemicKnowledgeDataService knowledgeDataService;
    private final EpidemicRiskAreasDataService riskAreasDataService;
    private final EpidemicAccessFlowService accessFlowService;

    @ApiOperation("H5查询最近一次疫情数据")
    @GetMapping("/lastEpidemic")
    public ResultBean<EpidemicBaseData> lastEpidemic( @RequestParam(required = false) @ApiParam("部门id") Long deptId) {

        return ResultBean.buildSuccess(baseDataService.lastEpidemic(deptId));
    }

    @ApiOperation("H5防疫科普列表查询")
    @GetMapping("/findKnowledgeList")
    public ResultBean<IPage<EpidemicKnowledgeData>> epidemicKnowledgeList(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                                          @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                                          @RequestParam(required = false) @ApiParam("标题") String title,
                                                                          @RequestParam @ApiParam("部门id") Long deptId,
                                                                          @RequestParam(required = false) @ApiParam("不传为全部，0为未发布，1为1发布") Integer type) {
        Page<EpidemicKnowledgeData> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return ResultBean.buildSuccess(knowledgeDataService.epidemicKnowledgeList(page,type,title,deptId));
    }

    @ApiOperation("H5风险地区人员进出列表")
    @GetMapping("/findRiskAreaList")
    public ResultBean<IPage<EpidemicRiskAreasData>> findRiskAreaList(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                                     @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                                     @RequestParam(required = false) @ApiParam("部门id") Long deptId,
                                                                     @RequestParam(required = false) @ApiParam("姓名") String name,
                                                                     @RequestParam(required = false) @ApiParam("部门名字模糊查询") String dept,
                                                                     @RequestParam(required = false) @ApiParam("地址") String address) {
        Page<EpidemicRiskAreasData> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return ResultBean.buildSuccess(riskAreasDataService.findRiskAreaList(page,name,dept,address,deptId));
    }

    @ApiOperation("后台实时人流量列表")
    @GetMapping("/findAccessFlowList")
    public ResultBean<IPage<EpidemicAccessFlow>> findAccessFlowList(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                                    @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                                    @RequestParam(required = false) @ApiParam("部门id") Long deptId,
                                                                    @RequestParam(required = false) @ApiParam("时间整点数量") Integer num,
                                                                    @RequestParam(required = false) @ApiParam("进出类型：0教师，1学生，2访客") Integer type) {
        Page<EpidemicAccessFlow> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return ResultBean.buildSuccess(accessFlowService.findAccessFlowList(page, num,type,deptId));
    }

    @ApiOperation("h5实时整点人流量列表")
    @GetMapping("/h5FindAccessFlowList")
    public ResultBean<List<EpidemicAccessFlow>> h5FindAccessFlowList(@RequestParam(required = true) @ApiParam("部门id") Long deptId,
                                                                     @RequestParam(required = false,defaultValue = "12") @ApiParam("时间整点数量,默认12个") Integer num,
                                                                     @RequestParam(required = false) @ApiParam("进出类型：0教师，1学生，2访客") Integer type) {

        return ResultBean.buildSuccess(accessFlowService.h5FindAccessFlowList(num,type,deptId));
    }

    @ApiOperation("H5查询疫情基础数据列表")
    @GetMapping("/findEpidemicList")
    public ResultBean<IPage<EpidemicBaseData>> findEpidemicList(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                                @RequestParam(required = false) @ApiParam("部门id") Long deptId,
                                                                @RequestParam(required = false, defaultValue = "10") Integer pageSize) {

        Page<EpidemicBaseData> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return ResultBean.buildSuccess(baseDataService.page(page,Wrappers.query(new EpidemicBaseData()).lambda().orderByDesc(EpidemicBaseData::getCreateDate).eq(EpidemicBaseData::getDeptId,deptId)));
    }

    @ApiOperation("新增或修改疫情基础数据")
    @PostMapping("/saveOrUpdateEpidemic")
    public ResultBean saveOrUpdateEpidemic(@RequestBody EpidemicBaseData baseData) {

        baseDataService.saveOrUpdate(baseData);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("删除疫情基础数据")
    @GetMapping("/removeEpidemic")
    public ResultBean removeEpidemic(@RequestParam Long id) {

        baseDataService.removeById(id);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询疫情基础数据")
    @GetMapping("/findEpidemic")
    public ResultBean<EpidemicBaseData> findEpidemic(@RequestParam Long id) {

        return ResultBean.buildSuccess(baseDataService.getById(id));
    }

    @ApiOperation("新增或修改疫情科普知识")
    @PostMapping("/saveOrUpdateKnowledge")
    public ResultBean saveOrUpdateKnowledge(@RequestBody EpidemicKnowledgeData baseData) {

        knowledgeDataService.saveOrUpdate(baseData);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("删除疫情科普知识")
    @GetMapping("/removeKnowledge")
    public ResultBean removeKnowledge(@RequestParam Long id) {

        knowledgeDataService.removeById(id);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("发布疫情科普知识")
    @GetMapping("/openKnowledge")
    public ResultBean openKnowledge(@RequestParam Long id) {
        EpidemicKnowledgeData epidemicKnowledgeData = new EpidemicKnowledgeData();
        epidemicKnowledgeData.setId(id);
        epidemicKnowledgeData.setType(1);
        knowledgeDataService.updateById(epidemicKnowledgeData);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询疫情科普知识")
    @GetMapping("/findKnowledge")
    public ResultBean<EpidemicKnowledgeData> findKnowledge(@RequestParam Long id) {

        return ResultBean.buildSuccess(knowledgeDataService.getById(id));
    }
    @ApiOperation("新增或修改风险地区人员")
    @PostMapping("/saveOrUpdateRiskArea")
    public ResultBean saveOrUpdateRiskArea(@RequestBody EpidemicRiskAreasData baseData) {

        riskAreasDataService.saveOrUpdate(baseData);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("删除风险地区人员")
    @GetMapping("/removeRiskArea")
    public ResultBean removeRiskAreas(@RequestParam Long id) {

        riskAreasDataService.removeById(id);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询风险地区人员")
    @GetMapping("/findRiskArea")
    public ResultBean<EpidemicRiskAreasData> findRiskAreas(@RequestParam Long id) {

        return ResultBean.buildSuccess(riskAreasDataService.getById(id));
    }
    @ApiOperation("新增或修改实时人流量")
    @PostMapping("/saveOrUpdateAccessFlow")
    public ResultBean saveOrUpdateAccessFlow(@RequestBody EpidemicAccessFlow baseData) {

        accessFlowService.saveOrUpdate(baseData);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("删除实时人流量数据")
    @GetMapping("/removeAccessFlow")
    public ResultBean removeAccessFlow(@RequestParam Long id) {

        accessFlowService.removeById(id);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询实时人流量数据")
    @GetMapping("/findAccessFlow")
    public ResultBean<EpidemicAccessFlow> findAccessFlow(@RequestParam Long id) {

        return ResultBean.buildSuccess(accessFlowService.getById(id));
    }
}
