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

import com.yihu.jw.base.service.course.RecruitStudentsRecordService;
import com.yihu.jw.entity.care.course.RecruitStudentsRecordDO;
import com.yihu.jw.hospital.dict.WlyyHospitalSysDictDao;
import com.yihu.jw.restmodel.web.Envelop;
import com.yihu.jw.restmodel.web.ListEnvelop;
import com.yihu.jw.restmodel.web.ObjEnvelop;
import com.yihu.jw.restmodel.web.PageEnvelop;
import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
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.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yeshijie
 * @Date: 2021/5/12
 * @Description:
 */
@RestController
@RequestMapping(value = "recruitStudentsRecord", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(value = "报名人员管理", description = "报名人员管理服务接口", tags = {"care基础服务 - 报名人员管理服务接口"})
public class RecruitStudentsRecordEndpoint extends EnvelopRestEndpoint {

    @Autowired
    private RecruitStudentsRecordService recruitStudentsRecordService;
    @Autowired
    private WlyyHospitalSysDictDao wlyyHospitalSysDictDao;

    @PostMapping(value = "create")
    @ApiOperation(value = "创建")
    public ObjEnvelop<RecruitStudentsRecordDO> create (
            @ApiParam(name = "jsonData", value = "Json数据", required = true)
            @RequestParam String jsonData) throws Exception {
        RecruitStudentsRecordDO recordDO = toEntity(jsonData, RecruitStudentsRecordDO.class);
        recordDO = recruitStudentsRecordService.save(recordDO);
        return success(recordDO);
    }

    @PostMapping(value = "delete")
    @ApiOperation(value = "删除")
    public Envelop delete(
            @ApiParam(name = "ids", value = "id串，中间用,分隔", required = true)
            @RequestParam(value = "ids") String ids) {
        recruitStudentsRecordService.delete(ids.split(","));
        return success("删除成功");
    }

    @PostMapping(value = "update")
    @ApiOperation(value = "更新")
    public ObjEnvelop<RecruitStudentsRecordDO> update (
            @ApiParam(name = "jsonData", value = "Json数据", required = true)
            @RequestParam String jsonData) throws Exception {
        RecruitStudentsRecordDO recordDO = toEntity(jsonData, RecruitStudentsRecordDO.class);
        if (null == recordDO.getId()) {
            return failed("ID不能为空", ObjEnvelop.class);
        }
        recordDO = recruitStudentsRecordService.save(recordDO);
        return success(recordDO);
    }

    @GetMapping(value = "page")
    @ApiOperation(value = "获取分页")
    public PageEnvelop<RecruitStudentsRecordDO> 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) throws Exception {
        List<RecruitStudentsRecordDO> recordDOs = recruitStudentsRecordService.search(fields, filters, sorts, page, size);
        int count = (int)recruitStudentsRecordService.getCount(filters);
        return success(recordDOs, count, page, size, RecruitStudentsRecordDO.class);
    }

    @GetMapping(value = "list")
    @ApiOperation(value = "获取列表")
    public ListEnvelop<RecruitStudentsRecordDO> list (
            @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<RecruitStudentsRecordDO> recordDOs = recruitStudentsRecordService.search(fields, filters, sorts);
        return success(recordDOs, RecruitStudentsRecordDO.class);
    }

    @PostMapping(value = "refund")
    @ApiOperation(value = "退费")
    public Envelop refund(
            @ApiParam(name = "id", value = "id", required = true)
            @RequestParam(value = "id") String id) {
        try{
            recruitStudentsRecordService.refund(id);
            return success("退费成功");
        }catch (Exception e){
            e.printStackTrace();
            return Envelop.getError("退费失败",-1);
        }

    }

    @PostMapping(value = "leaveSchool")
    @ApiOperation(value = "退学")
    public Envelop leaveSchool(
            @ApiParam(name = "id", value = "id", required = true)
            @RequestParam(value = "id") String id) {
        try{
            recruitStudentsRecordService.leaveSchool(id);
            return success("退学成功");
        }catch (Exception e){
            e.printStackTrace();
            return Envelop.getError("退学失败",-1);
        }

    }


    @PostMapping(value = "admissions")
    @ApiOperation(value = "录取")
    public Envelop admissions(
            @ApiParam(name = "ids", value = "id串，中间用,分隔", required = true)
            @RequestParam(value = "ids") String ids,
            @ApiParam(name = "recruitStudentsId", value = "招生记录id", required = true)
            @RequestParam(value = "recruitStudentsId") String recruitStudentsId) {
        try{
            String re =  recruitStudentsRecordService.admissions(ids,recruitStudentsId);
            if(re == null){
                return success("录取成功");
            }else {
                return Envelop.getError(re,-1);
            }
        }catch (Exception e){
            e.printStackTrace();
            return Envelop.getError("录取失败",-1);
        }
    }


    @PostMapping(value = "refuses")
    @ApiOperation(value = "批量拒绝")
    public Envelop refuses(
            @ApiParam(name = "ids", value = "id串，中间用,分隔", required = true)
            @RequestParam(value = "ids") String ids) {
        try{
            recruitStudentsRecordService.refuses(ids);
            return success("拒绝成功");
        }catch (Exception e){
            e.printStackTrace();
            return Envelop.getError("拒绝失败",-1);
        }

    }
}
