package com.bonus.bracelet.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.bonus.bracelet.mapper.QueryConstructionMapper;
import com.bonus.bracelet.service.IQueryConstructionService;
import com.bonus.common.core.utils.ExcelStyleUtil;
import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.entity.bracelet.exportVo.QueryConstructionExportVo;
import com.bonus.common.entity.bracelet.vo.PersonVo;
import com.bonus.common.entity.bracelet.vo.QueryConstruction;
import com.bonus.common.entity.bracelet.vo.SidebandDeviceVo;
import com.bonus.common.entity.bracelet.vo.WarnInfoVo;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.common.security.annotation.RequiresPermissions;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 岗位信息操作处理
 * 
 * @author bonus
 */
@RestController
@RequestMapping("/construction")
@Slf4j
public class QueryConstructionController extends BaseController
{
    @Autowired
    private IQueryConstructionService constructionService;

    @Autowired
    private QueryConstructionMapper constructionMapper;
    /**
     * 获取项目列表
     */
    @RequiresPermissions("bracelet:construction:list")
    @GetMapping("/list")
    @SysLog(title = "施工记录管理", businessType = OperaType.QUERY,module = "综合查询->施工记录管理")
    public TableDataInfo list(QueryConstruction construction) {
       try{
           startPage();
           List<QueryConstruction> list = constructionService.selectConstructionList(construction);
           return getDataTable(list);
       }catch (Exception e){
           log.error(e.toString(),e);
       }
       return  getDataTableError(new ArrayList<>());
    }

    /**
     * 班组人员设备列表
     * @param data
     * @return
     */
    @GetMapping("listTeam")
    @SysLog(title = "综合查询", businessType = OperaType.QUERY,  module = "综合查询->施工记录管理", details = "查询班组人员设备列表")
    public TableDataInfo listTeam(PersonVo data) {
        startPage();
        List<PersonVo> list = constructionService.getTeamLists(data);
        return getDataTable(list);
    }

    /**
     * 临时人员设备列表
     * @param data
     * @return
     */
    @GetMapping("listTeamTemp")
    @SysLog(title = "综合查询", businessType = OperaType.QUERY,  module = "综合查询->施工记录管理", details = "查询临时人员设备列表")
    public TableDataInfo listTeamTemp(PersonVo data) {
        startPage();
        List<PersonVo> list = constructionService.getTempLists(data);
        return getDataTable(list);
    }

    @RequiresPermissions("bracelet:construction:export")
    @PostMapping("/export")
    @SysLog(title = "施工记录管理", businessType = OperaType.EXPORT,module = "综合查询->施工记录管理")
    public void export(HttpServletResponse response, QueryConstruction construction) {
        try{
            List<QueryConstruction> list = constructionService.selectConstructionList(construction);
            ExcelUtil<QueryConstruction> util = new ExcelUtil<QueryConstruction>(QueryConstruction.class);
            util.exportExcel(response, list, "项目部数据");
        }catch (Exception e){
            log.error(e.toString(),e);
        }
    }

    /**
     * 施工预警信息列表
     * @param data
     * @return
     */
    @GetMapping("listWarn")
    @SysLog(title = "综合查询", businessType = OperaType.QUERY,  module = "综合查询->施工记录管理", details = "查询施工预警信息列表")
    public TableDataInfo listWarn(QueryConstruction data) {
        startPage();
        List<WarnInfoVo> list = constructionService.getWarnLists(data);
        return getDataTable(list);
    }

    @GetMapping("exportConstructionRecordSome")
    public void exportConstructionRecordSome(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "ids") String ids) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            List<QueryConstructionExportVo> queryConstructionExportVoList = new ArrayList<>();
            String[] idAll = ids.split(",");
            int[] num = new int[idAll.length];
            for (int i = 0; i < idAll.length; i++) {
                num[i] = Integer.parseInt(idAll[i]);
            }
            List<QueryConstruction> queryConstructionLists = constructionMapper.getConstructionSomeLists(num);
            for (int i = 0; i < queryConstructionLists.size(); i++) {
                QueryConstructionExportVo exportVo = new QueryConstructionExportVo();
                QueryConstruction vo = queryConstructionLists.get(i);
                BeanUtils.copyProperties(vo, exportVo);
                exportVo.setId((i + 1) + "");
                exportVo.setTeamLeader(vo.getTeamLeader()+ "\\" +vo.getTeamPhone());
                exportVo.setLyTime(sdf.format(vo.getLyTime()));
                queryConstructionExportVoList.add(exportVo);
            }
            ExportParams exportParams = new ExportParams("施工记录", "施工记录", ExcelType.XSSF);
            exportParams.setStyle(ExcelStyleUtil.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, QueryConstructionExportVo.class, queryConstructionExportVoList);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("施工记录" + ".xlsx", "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.close();
            workbook.close();
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
    }
}
