package com.yihu.jw.base.endpoint.voluntary;

import com.yihu.jw.base.dao.voluntary.VoluntaryRecruitmentCompanyDao;
import com.yihu.jw.base.dao.voluntary.VoluntaryRecruitmentPeopleDao;
import com.yihu.jw.base.service.voluntary.VoluntaryRecruitmentArrangeService;
import com.yihu.jw.base.service.voluntary.VoluntaryRecruitmentCompanyService;
import com.yihu.jw.base.service.voluntary.VoluntaryRecruitmentPeopleService;
import com.yihu.jw.base.service.voluntary.VoluntaryRecruitmentService;
import com.yihu.jw.entity.voluntary.VoluntaryRecruitmentCompanyDO;
import com.yihu.jw.entity.voluntary.VoluntaryRecruitmentPeopleDO;
import com.yihu.jw.hospital.dict.service.WlyyHospitalSysDictService;
import com.yihu.jw.restmodel.web.*;
import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
import com.yihu.jw.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yeshijie
 * @Date: 2021/8/1
 * @Description:
 */
@RestController
@RequestMapping(value = "open/voluntary")
@Api(value = "新冠-志愿招募管理", description = "新冠-志愿招募管理", tags = {"新冠-志愿招募管理"})
public class VoluntaryRecruitmentEndpoint extends EnvelopRestEndpoint {
 
    @Autowired
    private VoluntaryRecruitmentService voluntaryRecruitmentService;
    @Autowired
    private VoluntaryRecruitmentCompanyService companyService;
    @Autowired
    private VoluntaryRecruitmentPeopleDao peopleDao;
    @Autowired
    private WlyyHospitalSysDictService wlyyHospitalSysDictService;
    @Autowired
    private VoluntaryRecruitmentArrangeService arrangeService;
    @Autowired
    private VoluntaryRecruitmentPeopleService peopleService;
    @Autowired
    private VoluntaryRecruitmentCompanyDao companyDao;

    @PostMapping(value = "add")
    @ApiOperation(value = "企业报名")
    public Envelop add (
            @ApiParam(name = "jsonData", value = "Json数据", required = true)
         @RequestParam String jsonData){
        try {
            VoluntaryRecruitmentCompanyDO companyDO = toEntity(jsonData, VoluntaryRecruitmentCompanyDO.class);
            String result = voluntaryRecruitmentService.add(companyDO);
            if(StringUtil.isBlank(result)){
                return success("报名成功");
            }
           return Envelop.getError(result,-1);
        }catch (Exception e){
            e.printStackTrace();
            return failedException2(e);
        }
    }

    @GetMapping(value = "page")
    @ApiOperation(value = "获取分页")
    public PageEnvelop<VoluntaryRecruitmentCompanyDO> page (
            @ApiParam(name = "fields", value = "返回的字段，为空返回全部字段")
            @RequestParam(value = "fields", required = false) String fields,
            @ApiParam(name = "filters", value = "过滤器，为空检索所有条件")
            @RequestParam(value = "filters", required = false) String filters,
            @ApiParam(name = "sorts", value = "排序，规则参见说明文档")
            @RequestParam(value = "sorts", required = false) String sorts,
            @ApiParam(name = "page", value = "分页大小", required = true, defaultValue = "1")
            @RequestParam(value = "page") int page,
            @ApiParam(name = "size", value = "页码", required = true, defaultValue = "15")
            @RequestParam(value = "size") int size){
            try {
                List<VoluntaryRecruitmentCompanyDO> appVersions = companyService.search(fields, filters, sorts, page, size);
                int count = (int)companyService.getCount(filters);
                return success(appVersions, count, page, size, VoluntaryRecruitmentCompanyDO.class);
            }catch (Exception e){
                e.printStackTrace();
                return  PageEnvelop.getError("查询失败");
            }
    }

    @GetMapping(value = "existsByPhone")
    @ApiOperation(value = "人员是否已报名")
    public ObjEnvelop existsByPhone (
            @ApiParam(name = "phone", value = "联系人手机")
            @RequestParam(value = "phone", required = true) String phone){
        try {
            return ObjEnvelop.getSuccess("查找成功",peopleDao.existsByPhone(phone));
        }catch (Exception e){
            e.printStackTrace();
            return  ObjEnvelop.getError("查询失败");
        }
    }

    @GetMapping(value = "findByContactsAndPhone")
    @ApiOperation(value = "查找单个企业")
    public ObjEnvelop<VoluntaryRecruitmentCompanyDO> findByContactsAndPhone (
            @ApiParam(name = "contacts", value = "联系人姓名")
            @RequestParam(value = "contacts", required = true) String contacts,
            @ApiParam(name = "phone", value = "联系人手机")
            @RequestParam(value = "phone", required = true) String phone){
        try {
            VoluntaryRecruitmentCompanyDO companyDO = voluntaryRecruitmentService.findByContactsAndPhone(contacts,phone);
            if(companyDO==null){
               return ObjEnvelop.getError("未找到关联企业");
            }
            return ObjEnvelop.getSuccess("查找成功",companyDO);
        }catch (Exception e){
            e.printStackTrace();
            return  ObjEnvelop.getError("查询失败");
        }
    }

    @PostMapping(value = "arrange")
    @ApiOperation(value = "人员安排")
    public Envelop arrange (
            @ApiParam(name = "companyId", value = "联系人姓名")
            @RequestParam(value = "companyId", required = true) String companyId,
            @ApiParam(name = "day", value = "服务日期")
            @RequestParam(value = "day", required = true) String day,
            @ApiParam(name = "time", value = "服务时间段")
            @RequestParam(value = "time", required = true) String time,
            @ApiParam(name = "peoples", value = "服务人员 id+姓名逗号间隔。例：1:测试1,2:测试2")
            @RequestParam(value = "peoples", required = true) String peoples,
            @ApiParam(name = "station", value = "服务站")
            @RequestParam(value = "station", required = true) String station){
        try {
            voluntaryRecruitmentService.arrange(companyId, day, time, peoples, station);
            return success("安排成功");
        }catch (Exception e){
            e.printStackTrace();
            return failedException2(e);
        }
    }

    @GetMapping(value = "arrangeInfoList")
    @ApiOperation(value = "获取企业人员安排数量列表")
    public ListEnvelop arrangeInfoList (
            @ApiParam(name = "companyId", value = "企业id")
            @RequestParam(value = "companyId", required = true) String companyId){
        try {
            return ListEnvelop.getSuccess("安排成功",voluntaryRecruitmentService.arrangeInfoList(companyId));
        }catch (Exception e){
            return failedListEnvelopException2(e);
        }
    }

    @GetMapping(value = "arrangeInfoDetail")
    @ApiOperation(value = "获取企业安排信息详情详情")
    public ListEnvelop arrangeInfoDetail (
            @ApiParam(name = "companyId", value = "企业id")
            @RequestParam(value = "companyId", required = true) String companyId,
            @ApiParam(name = "day", value = "日期 yyyy-MM-dd格式")
            @RequestParam(value = "day", required = false) String day){
        try {
            return ListEnvelop.getSuccess("安排成功",voluntaryRecruitmentService.arrangeInfoDetail(companyId,day));
        }catch (Exception e){
            return failedListEnvelopException2(e);
        }
    }

    @GetMapping(value = "companyPeopleUnArrange")
    @ApiOperation(value = "获取企业志愿者列表(未被安排)")
    public ListEnvelop companyPeopleUnArrange (
            @ApiParam(name = "companyId", value = "企业id")
            @RequestParam(value = "companyId", required = true) String companyId,
            @ApiParam(name = "day", value = "日期 yyyy-MM-dd格式")
            @RequestParam(value = "day", required = true) String day,
            @ApiParam(name = "time", value = "时间")
            @RequestParam(value = "time", required = true) String time){
        try {
            return ListEnvelop.getSuccess("安排成功",voluntaryRecruitmentService.companyPeopleUnArrange(companyId,day,time));
        }catch (Exception e){
            return failedListEnvelopException2(e);
        }
    }

    @PostMapping(value = "arrangeDelete")
    @ApiOperation(value = "企业志愿者安排删除")
    public Envelop arrangeDelete(
            @ApiParam(name = "ids", value = "id串，中间用,分隔", required = true)
            @RequestParam(value = "ids") String ids) {
        arrangeService.delete(ids.split(","));
        return success("删除成功");
    }

    @GetMapping(value = "companyPeopleList")
    @ApiOperation(value = "企业志愿者名单列表")
    public ListEnvelop<VoluntaryRecruitmentPeopleDO> companyPeopleList (
            @ApiParam(name = "fields", value = "返回的字段，为空返回全部字段")
            @RequestParam(value = "fields", required = false) String fields,
            @ApiParam(name = "filters", value = "过滤器，为空检索所有条件")
            @RequestParam(value = "filters", required = false) String filters,
            @ApiParam(name = "sorts", value = "排序，规则参见说明文档")
            @RequestParam(value = "sorts", required = false) String sorts) throws Exception {
        List<VoluntaryRecruitmentPeopleDO> appVersions = peopleService.search(fields, filters, sorts);
        return success(appVersions, VoluntaryRecruitmentPeopleDO.class);
    }

    @PostMapping(value = "companyPeopleDelete")
    @ApiOperation(value = "企业志愿者名单删除")
    public Envelop companyPeopleDelete(
            @ApiParam(name = "ids", value = "id串，中间用,分隔", required = true)
            @RequestParam(value = "ids") String ids) {
        String[] idsTmp = ids.split(",");
        List<VoluntaryRecruitmentPeopleDO> List = peopleService.getRepository().queryByIdIn(idsTmp);
        peopleService.delete(ids.split(","));
        for (VoluntaryRecruitmentPeopleDO tmp:List){
            VoluntaryRecruitmentCompanyDO companyDO = companyDao.findOne(tmp.getCompanyId());
            if (null !=companyDO ){
                Integer count = peopleService.getRepository().findByCompanyId(tmp.getCompanyId());
                companyDO.setNum(count);
                companyDao.save(companyDO);
            }
        }
        return success("删除成功");
    }

    @ApiOperation("查询字典")
    @GetMapping(value= "findDict")
    public MixEnvelop findDict(@ApiParam(name = "name", value = "name", required = false)
                               @RequestParam(value = "name", required = false)String name,
                               @ApiParam(name = "modelName", value = "modelName", required = false)
                               @RequestParam(value = "modelName", required = false)String modelName,
                               @ApiParam(name = "code", value = "code", required = false)
                               @RequestParam(value = "code", required = false)String code,
                               @ApiParam(name = "value", value = "value", required = false)
                               @RequestParam(value = "value", required = false)String value,
                               @ApiParam(name = "page", value = "page", required = false)
                               @RequestParam(value = "page", required = false) Integer page,
                               @ApiParam(name = "pageSize", value = "pageSize", required = false)
                               @RequestParam(value = "pageSize", required = false)Integer pageSize){

        return wlyyHospitalSysDictService.findDictsByNameCode(modelName,name,code,value,page,pageSize);
    }

}
