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

import com.yihu.jw.base.dao.voluntary.VoluntaryRecruitmentPeopleDao;
import com.yihu.jw.base.service.voluntary.VoluntaryRecruitmentArrangeService;
import com.yihu.jw.entity.voluntary.VoluntaryRecruitmentArrangeDO;
import com.yihu.jw.entity.voluntary.VoluntaryRecruitmentPeopleDO;
import com.yihu.jw.restmodel.web.Envelop;
import com.yihu.jw.restmodel.web.ListEnvelop;
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 jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

/**
 * Created by Bing on 2021/8/4.
 */
@RestController
@RequestMapping(value = "voluntaryRecruitmentArrange")
@Api(value = "新冠-志愿招募安排管理", description = "新冠-志愿招募安排管理", tags = {"新冠-志愿招募安排管理"})
public class VoluntaryRecruitmentArrangeEndpoint extends EnvelopRestEndpoint {

    @Autowired
    private VoluntaryRecruitmentArrangeService arrangeService;
    @Autowired
    private VoluntaryRecruitmentPeopleDao peopleDao;

    @GetMapping(value = "page")
    @ApiOperation(value = "获取分页")
    public PageEnvelop<VoluntaryRecruitmentArrangeDO> 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<VoluntaryRecruitmentArrangeDO> appVersions = arrangeService.search(fields, filters, sorts, page, size);
        int count = (int) arrangeService.getCount(filters);
        return success(appVersions, count, page, size, VoluntaryRecruitmentArrangeDO.class);
    }

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


    @GetMapping(value = "/excelControl/exportExcel")
    @ApiOperation("导出列表")
    @ResponseBody
    public void searchList(
            @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,
            HttpServletResponse response) {
        try {
            List<VoluntaryRecruitmentArrangeDO> appVersions = arrangeService.search(fields, filters, sorts);
            response.setContentType("octets/stream");
            response.setHeader("Content-Disposition", "attachment; filename=" + new String("volunteerArrange.xls"));
            OutputStream os = response.getOutputStream();
            this.write(os, appVersions);
        } catch (Exception ex) {
            Envelop.getError("导出失败");
        }
    }

    private void write(OutputStream os, List<VoluntaryRecruitmentArrangeDO> list) throws Exception {
        WritableWorkbook wwb = Workbook.createWorkbook(os);
        try {
            WritableSheet sheet = wwb.createSheet("sheet", 1);
            String header[] = {"序号", "日期","服务时间","所在单位", " 姓名", "手机号", "身份证号", "是否有核酸检测信息采集设备运维经验","是否接种疫苗"};
            int i = 0;
            for (String h : header) {
                addCell(sheet, 0, i, h);
                i++;
            }
            int j = 1;
            int ii=1;
            for (VoluntaryRecruitmentArrangeDO tmp : list) {
                VoluntaryRecruitmentPeopleDO peopleDO= peopleDao.findOne(tmp.getPeopleId());
                if (null == peopleDO){
                    continue;
                }
                String vaccinationName = "未接种";
                String experienceName = "否";
                if (StringUtils.isNotBlank(peopleDO.getVaccination())){
                    String  vaccination = peopleDO.getVaccination();
                    switch (vaccination){
                        case "1":
                            vaccinationName = "已接种一针";
                            break;
                        case "2":
                            vaccinationName = "已接种";
                            break;
                    }
                }
                if (StringUtils.isNotBlank(peopleDO.getExperience())){
                    String  experience = peopleDO.getExperience();
                    switch (experience){
                        case "0":
                            experienceName = "否";
                            break;
                        case "1 ":
                            experienceName = "是";
                            break;
                    }
                }
                addCell(sheet, j, 0, ii + "");
                addCell(sheet, j, 1, tmp.getDay());
                addCell(sheet, j, 2, tmp.getTime());
                addCell(sheet, j, 3, peopleDO.getCompanyName());
                addCell(sheet, j, 4, tmp.getPeopleName() + "");
                addCell(sheet, j, 5, peopleDO.getPhone()+"");
                addCell(sheet, j, 6, peopleDO.getIdcard() + "");
                addCell(sheet, j, 7, experienceName);
                addCell(sheet, j, 8, vaccinationName);
                j++;
                ii++;
            }
            wwb.write();
            wwb.close();
        } catch (Exception e) {
            e.printStackTrace();
            if (wwb != null) wwb.close();
        }
    }

    private void addCell(WritableSheet ws, int row, int column, String data) throws WriteException {
        Label label = new Label(column, row, data);
        ws.addCell(label);
    }

}
