package com.hmit.kernes.controller;

import com.hmit.kernes.entity.ApEntityForFile;
import com.hmit.kernes.entity.ApplyCenterEntity;
import com.hmit.kernes.service.ApplyCenterService;
import com.hmit.kernes.utils.*;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.IntrospectionException;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.*;

@Controller
@RequestMapping("/apply/center")
public class ApplyCenterController {
    @Autowired
    ApplyCenterService applyCenterService;
    @ResponseBody
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params, String id){
        //查询列表数据
        if(!StringUtils.isBlank(id)){
             params.put("apType", id);
        }
        Query query = new Query(params);
        List<ApplyCenterEntity> userList = applyCenterService.queryList(query);
        int total = applyCenterService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }
    @ResponseBody
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
        ApplyCenterEntity ap = applyCenterService.queryObject(id);

        return R.ok().put("ap", ap);
    }

    @ResponseBody
    @RequestMapping("/save")
    public R save(@RequestBody ApplyCenterEntity ap){

        verify(ap);

        applyCenterService.save(ap);

        return R.ok();
    }

    @ResponseBody
    @RequestMapping("/update")
    public R update(@RequestBody ApplyCenterEntity ap){

        verify(ap);

        applyCenterService.update(ap);

        return R.ok();
    }

    @ResponseBody
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
       applyCenterService.deleteBatch(ids);

        return R.ok();
    }

    @ResponseBody
    @RequestMapping("/output")
    public R  output(@RequestParam String id, HttpServletRequest request, HttpServletResponse response)throws ClassNotFoundException, IntrospectionException, IllegalAccessException, ParseException, InvocationTargetException {
        int idx=Integer.parseInt(id);
        response.reset();
        List<ApplyCenterEntity> listOfAp=applyCenterService.queryListByType(idx);
        XSSFWorkbook xssfWorkbook=null;
        xssfWorkbook=exportExcelInfo(listOfAp);
        OutputStream output;


        response.setHeader("Content-disposition", "attachment; filename=details.xlsx");
        response.setContentType("application/msexcel");
        try {
            output = response.getOutputStream();
            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
           bufferedOutPut.flush();
            xssfWorkbook.write(bufferedOutPut);

            bufferedOutPut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return  R.ok();


    }

    public XSSFWorkbook exportExcelInfo(List<ApplyCenterEntity> listOfAp) throws InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {
        List<ApEntityForFile> listApF=new ArrayList<>();
        for(int i=0;i<listOfAp.size();i++){
            ApEntityForFile apEntityForFile=new ApEntityForFile(listOfAp.get(i));
            listApF.add(apEntityForFile);

        }



        List<ExcelBean> excel=new ArrayList<>();
        Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
        XSSFWorkbook xssfWorkbook=null;
        if(listOfAp.get(0).getApType()==20101) {
            excel.add(new ExcelBean("序号", "id", 0));
            excel.add(new ExcelBean("类型", "apTypeForFile", 1));
            excel.add(new ExcelBean("状态", "apStatusForFile", 2));
            excel.add(new ExcelBean("提交日期", "apApplyTime", 3));
            excel.add(new ExcelBean("性别", "apSexualForFile", 4));
            excel.add(new ExcelBean("姓名", "apPersonName", 5));
            excel.add(new ExcelBean("生日", "apBirthday", 6));
            excel.add(new ExcelBean("电话", "apTel", 7));
            excel.add(new ExcelBean("五一服务卡号", "apCard", 8));
            excel.add(new ExcelBean("地址", "apAddress", 9));
            excel.add(new ExcelBean("申请用途", "apWill", 10));

        }else if(listOfAp.get(0).getApType()==20102){
            excel.add(new ExcelBean("序号", "id", 0));
            excel.add(new ExcelBean("类型", "apTypeForFile", 1));
            excel.add(new ExcelBean("状态", "apStatusForFile", 2));
            excel.add(new ExcelBean("提交日期", "apApplyTime", 3));
            excel.add(new ExcelBean("性别", "apSexualForFile", 4));
            excel.add(new ExcelBean("姓名", "apPersonName", 5));
            excel.add(new ExcelBean("生日", "apBirthday", 6));
            excel.add(new ExcelBean("电话", "apTel", 7));
            excel.add(new ExcelBean("工作单位", "apWork", 8));
            excel.add(new ExcelBean("五一服务卡号", "apCard", 9));
            excel.add(new ExcelBean("申请事由", "apReason", 10));
        }else if(listOfAp.get(0).getApType()==20103){
            excel.add(new ExcelBean("序号", "id", 0));
            excel.add(new ExcelBean("类型", "apTypeForFile", 1));
            excel.add(new ExcelBean("状态", "apStatusForFile", 2));
            excel.add(new ExcelBean("提交日期", "apApplyTime", 3));
            excel.add(new ExcelBean("联系人", "apPersonName", 4));
            excel.add(new ExcelBean("公司名称", "apCompanyName", 5));
            excel.add(new ExcelBean("电话", "apTel", 6));
            excel.add(new ExcelBean("地址", "apAddress", 7));
            excel.add(new ExcelBean("法人代表", "apCorporate", 8));
            excel.add(new ExcelBean("职工人数", "workerCount", 9));
            excel.add(new ExcelBean("会员人数", "memberCount", 10));
            excel.add(new ExcelBean("所在镇/街道", "apDistrict", 11));
        }else if(listOfAp.get(0).getApType()==20104){
            excel.add(new ExcelBean("序号", "id", 0));
            excel.add(new ExcelBean("类型", "apTypeForFile", 1));
            excel.add(new ExcelBean("状态", "apStatusForFile", 2));
            excel.add(new ExcelBean("提交日期", "apApplyTime", 3));
            excel.add(new ExcelBean("性别", "apSexualForFile", 4));
            excel.add(new ExcelBean("姓名", "apPersonName", 5));
            excel.add(new ExcelBean("生日", "apBirthday", 6));
            excel.add(new ExcelBean("身份证号", "apIdn", 7));
            excel.add(new ExcelBean("电话", "apTel", 8));
            excel.add(new ExcelBean("工作单位", "apWork", 9));
            excel.add(new ExcelBean("职务", "apJob", 10));
            excel.add(new ExcelBean("所在镇/街道", "apDistrict", 11));
            excel.add(new ExcelBean("籍贯", "apNative", 12));
            excel.add(new ExcelBean("文化程度", "apDegreeForFile", 13));
            excel.add(new ExcelBean("民族", "apNation", 14));
            excel.add(new ExcelBean("政治面貌", "apPoliticForFile", 15));
            excel.add(new ExcelBean("月工资收入", "apSalary", 16));

        }else if(listOfAp.get(0).getApType()==20105){
            excel.add(new ExcelBean("序号", "id", 0));
            excel.add(new ExcelBean("类型", "apTypeForFile", 1));
            excel.add(new ExcelBean("状态", "apStatusForFile", 2));
            excel.add(new ExcelBean("提交日期", "apApplyTime", 3));
            excel.add(new ExcelBean("性别", "apSexualForFile", 4));
            excel.add(new ExcelBean("姓名", "apPersonName", 5));
            excel.add(new ExcelBean("生日", "apBirthday", 6));
            excel.add(new ExcelBean("身份证号", "apIdn", 7));
            excel.add(new ExcelBean("电话", "apTel", 8));
            excel.add(new ExcelBean("工作单位", "apWork", 9));

        }else if(listOfAp.get(0).getApType()==20106){
            excel.add(new ExcelBean("序号", "id", 0));
            excel.add(new ExcelBean("类型", "apTypeForFile", 1));
            excel.add(new ExcelBean("状态", "apStatusForFile", 2));
            excel.add(new ExcelBean("提交日期", "apApplyTime", 3));
            excel.add(new ExcelBean("工会名称", "apCompanyName", 4));
            excel.add(new ExcelBean("电话", "apTel", 5));
            excel.add(new ExcelBean("地址", "apAddress", 6));
            excel.add(new ExcelBean("法人代表", "apCorporate", 7));
            excel.add(new ExcelBean("职工人数", "workerCount", 8));
            excel.add(new ExcelBean("会员人数", "memberCount", 9));
        }else if(listOfAp.get(0).getApType()==20108){
            excel.add(new ExcelBean("序号", "id", 0));
            excel.add(new ExcelBean("类型", "apTypeForFile", 1));
            excel.add(new ExcelBean("状态", "apStatusForFile", 2));
            excel.add(new ExcelBean("提交日期", "apApplyTime", 3));
            excel.add(new ExcelBean("性别", "apSexualForFile", 4));
            excel.add(new ExcelBean("姓名", "apPersonName", 5));
            excel.add(new ExcelBean("生日", "apBirthday", 6));
            excel.add(new ExcelBean("身份证号", "apIdn", 7));
            excel.add(new ExcelBean("电话", "apTel", 8));
            excel.add(new ExcelBean("五一服务卡号", "apCard", 9));
            excel.add(new ExcelBean("地址", "apAddress", 10));
            excel.add(new ExcelBean("籍贯", "apNative", 11));
            excel.add(new ExcelBean("文化程度", "apDegreeForFile", 12));
            excel.add(new ExcelBean("求职意向", "apIntent", 13));

        }else if(listOfAp.get(0).getApType()==20109){
            excel.add(new ExcelBean("序号", "id", 0));
            excel.add(new ExcelBean("类型", "apTypeForFile", 1));
            excel.add(new ExcelBean("状态", "apStatusForFile", 2));
            excel.add(new ExcelBean("提交日期", "apApplyTime", 3));
            excel.add(new ExcelBean("性别", "apSexualForFile", 4));
            excel.add(new ExcelBean("姓名", "apPersonName", 5));
            excel.add(new ExcelBean("身份证号", "apIdn", 6));
            excel.add(new ExcelBean("电话", "apTel", 7));
            excel.add(new ExcelBean("工作单位", "apWork", 8));
            excel.add(new ExcelBean("五一服务卡号", "apCard", 9));

        }else if(listOfAp.get(0).getApType()==20110||listOfAp.get(0).getApType()==20111){
            excel.add(new ExcelBean("序号", "id", 0));
            excel.add(new ExcelBean("类型", "apTypeForFile", 1));
            excel.add(new ExcelBean("状态", "apStatusForFile", 2));
            excel.add(new ExcelBean("提交日期", "apApplyTime", 3));
            excel.add(new ExcelBean("性别", "apSexualForFile", 4));
            excel.add(new ExcelBean("姓名", "apPersonName", 5));
            excel.add(new ExcelBean("身份证号", "apIdn", 6));
            excel.add(new ExcelBean("电话", "apTel", 7));
            excel.add(new ExcelBean("工作单位", "apWork", 8));
            excel.add(new ExcelBean("五一服务卡号", "apCard", 9));
            excel.add(new ExcelBean("申请事由", "apReason", 10));
            excel.add(new ExcelBean("地址", "apAddress", 11));

        }else{//以防万一,若aptype为空
            excel.add(new ExcelBean("序号","id",0));
            excel.add(new ExcelBean("类型","apTypeForFile",1));
            excel.add(new ExcelBean("状态","apStatusForFile",2));
            excel.add(new ExcelBean("提交日期","apApplyTime",3));
            excel.add(new ExcelBean("性别","apSexualForFile",4));
            excel.add(new ExcelBean("姓名","apPersonName",5));
            excel.add(new ExcelBean("公司名称","apCompanyName",6));
            excel.add(new ExcelBean("生日","apBirthday",7));
            excel.add(new ExcelBean("身份证号","apIdn",8));
            excel.add(new ExcelBean("电话","apTel",9));
            excel.add(new ExcelBean("工作单位","apWork",10));
            excel.add(new ExcelBean("职务","apJob",11));
            excel.add(new ExcelBean("五一服务卡号","apCard",12));
            excel.add(new ExcelBean("申请事由","apReason",13));
            excel.add(new ExcelBean("地址","apAddress",14));
            excel.add(new ExcelBean("法人代表","apCorporate",15));
            excel.add(new ExcelBean("职工人数","workerCount",16));
            excel.add(new ExcelBean("会员人数","memberCount",17));
            excel.add(new ExcelBean("所在镇/街道","apDistrict",18));
            excel.add(new ExcelBean("籍贯","apNative",19));
            excel.add(new ExcelBean("文化程度","apDegreeForFile",20));
            excel.add(new ExcelBean("求职意向","apWill",21));
            excel.add(new ExcelBean("民族","apNation",22));
            excel.add(new ExcelBean("政治面貌","apPoliticForFile" ,23));

        }


        map.put(0,excel);
        String sheetName="sheetByFancy";
        xssfWorkbook=ExcelUtil.createExcelFile(ApEntityForFile.class,listApF,map,sheetName);
        return xssfWorkbook;

    }



    private void verify(ApplyCenterEntity ap){
        if(ap.getApApplyTime()==null){
            ap.setApApplyTime(new Date());

        }


    }














}