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

import com.yihu.jw.base.service.voluntary.VoluntaryRecruitmentCompanyService;
import com.yihu.jw.entity.voluntary.VoluntaryRecruitmentCompanyDO;
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 jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import org.apache.http.client.utils.DateUtils;
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.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yeshijie
 * @Date: 2021/8/1
 * @Description:
 */
@RestController
@RequestMapping(value = "voluntaryRecruitmentCompany")
@Api(value = "新冠-志愿招募单位管理", description = "新冠-志愿招募单位管理", tags = {"新冠-志愿招募单位管理"})
public class VoluntaryRecruitmentCompanyEndpoint extends EnvelopRestEndpoint {

    @Autowired
    private VoluntaryRecruitmentCompanyService companyService;


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

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

    @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) throws Exception {
        List<VoluntaryRecruitmentCompanyDO> appVersions = companyService.search(fields, filters, sorts, page, size);
        int count = (int) companyService.getCount(filters);
        return success(appVersions, count, page, size, VoluntaryRecruitmentCompanyDO.class);
    }

    @GetMapping(value = "list")
    @ApiOperation(value = "获取列表")
    public ListEnvelop<VoluntaryRecruitmentCompanyDO> 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<VoluntaryRecruitmentCompanyDO> appVersions = companyService.search(fields, filters, sorts);
        return success(appVersions, VoluntaryRecruitmentCompanyDO.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<VoluntaryRecruitmentCompanyDO> appVersions = companyService.search(fields, filters, sorts);
            response.setContentType("octets/stream");
            response.setHeader("Content-Disposition", "attachment; filename=" + new String("volunteerInfo.xls"));
            OutputStream os = response.getOutputStream();
            this.write(os, appVersions);
        } catch (Exception ex) {
            Envelop.getError("导出失败");
        }
    }

    private void write(OutputStream os, List<VoluntaryRecruitmentCompanyDO> 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 (VoluntaryRecruitmentCompanyDO tmp : list) {
                addCell(sheet, j, 0, ii + "");
                addCell(sheet, j, 1, tmp.getName());
                addCell(sheet, j, 2, tmp.getContacts());
                addCell(sheet, j, 3, tmp.getPhone() + "");
                addCell(sheet, j, 4, tmp.getNum()+"");
                addCell(sheet, j, 5, tmp.getTimes() + "");
                addCell(sheet, j, 6, tmp.getServiceStation() + "");
                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);
    }

}
