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.EngineeringMapper;
import com.bonus.bracelet.service.IEngineeringService;
import com.bonus.common.core.utils.ExcelStyleUtil;
import com.bonus.common.core.utils.UploadCheckUtils;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.entity.bracelet.exportVo.EngineeringExportVo;
import com.bonus.common.entity.bracelet.exportVo.GtExportVo;
import com.bonus.common.entity.bracelet.vo.AttributeVo;
import com.bonus.common.entity.bracelet.vo.DeviceVo;
import com.bonus.common.entity.bracelet.vo.EngineeringVo;
import com.bonus.common.entity.bracelet.vo.GtVo;
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.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * EngineeringController
 */
@RestController
@RequestMapping("/engineering/")
@Slf4j
public class EngineeringController extends BaseController {

    @Resource(name = "IEngineeringService")
    private IEngineeringService service;

    @Resource(name = "EngineeringMapper")
    private EngineeringMapper Mapper;

    /**
     * 工程列表
     * @param data
     * @return
     */
    @GetMapping("list")
    @SysLog(title = "工程管理", businessType = OperaType.QUERY,  module = "基础管理->工程管理", details = "查询工程列表")
    public TableDataInfo list(EngineeringVo data) {
        startPage();
        List<EngineeringVo> list =service.getEngineeringLists(data);
        return getDataTable(list);
    }

    /**
     * 根据工程id获取杆塔详细信息
     */
    @GetMapping(value = "gtInfo")
    @SysLog(title = "工程管理", businessType = OperaType.QUERY,  module = "基础管理->工程管理", details = "查询杆塔列表")
    public TableDataInfo getGtInfo(GtVo vo) {
        startPage();
        List<GtVo> list =  service.selectGtById(vo);
        return getDataTable(list);
    }

    /**
     * 新增杆塔信息
     * @param vo
     * @return
     */
    @PostMapping("addGt")
    @SysLog(title = "工程管理", businessType = OperaType.INSERT,  module = "基础管理->工程管理", details = "新增杆塔")
    public AjaxResult addGt(@Validated @RequestBody GtVo vo) {
        return service.addGt(vo);
    }

    /**
     * 根据杆塔id获取详细信息
     */
    @GetMapping(value = "/{gtId}")
    public AjaxResult getGtId(@PathVariable Integer gtId) {
        return service.selectGtId(gtId);
    }

    /**
     * 修改杆塔信息
     * @param vo
     * @return
     */
    @PostMapping("editGt")
    @SysLog(title = "工程管理", businessType = OperaType.UPDATE, module = "基础管理->工程管理", details = "修改杆塔")
    public AjaxResult editGt(@Validated @RequestBody GtVo vo) {
        return service.editGt(vo);
    }

    /**
     * 删除杆塔
     */
    @PostMapping("delGt")
    @SysLog(title = "工程管理", businessType = OperaType.UPDATE,module = "基础管理->工程管理",details = "删除杆塔")
    public AjaxResult delGt(@RequestBody GtVo vo) {
            return service.deleteGt(vo.getGtId());
    }

    /**
     * 根据工程id获取详细信息
     */
    @GetMapping(value = "getEngineeringInfo/{proId}")
    public AjaxResult getEngineeringInfo(@PathVariable Long proId) {
        return service.getEngineeringInfo(proId);
    }

    /**
     * 修改工程信息
     * @param vo
     * @return
     */
    @PostMapping("editPro")
    @SysLog(title = "工程管理", businessType = OperaType.UPDATE, module = "基础管理->工程管理", details = "修改工程")
    public AjaxResult editPro(@Validated @RequestBody EngineeringVo vo) {
        return service.editPro(vo);
    }

    /**
     * 新增工程信息
     * @param vo
     * @return
     */
    @PostMapping("addPro")
    @SysLog(title = "工程管理", businessType = OperaType.INSERT,  module = "基础管理->工程管理", details = "新增工程")
    public AjaxResult addPro(@Validated @RequestBody EngineeringVo vo) {
        return service.addPro(vo);
    }

    /**
     * 删除工程
     */
    @PostMapping("delPro/{proId}")
    @SysLog(title = "工程管理", businessType = OperaType.UPDATE,module = "基础管理->工程管理",details = "删除工程")
    public AjaxResult delPro(@PathVariable Long proId) {
        return service.deletePro(proId);
    }

    @PostMapping("excelUpload")
    @SysLog(title = "工程管理", businessType = OperaType.IMPORT,logType = 0,module = "基础管理->工程管理",details ="工程导入" )
    public AjaxResult excelUpload(MultipartFile file, HttpServletRequest request, HttpServletResponse response,@RequestParam Long proId){
        String result = UploadCheckUtils.uploadExcelVerify(file);
        if(StringUtils.isNotBlank(result)){
            return AjaxResult.error(result);
        }
        return service.excelUpload(file, request, response,proId);
    }

    @GetMapping("exportEngineeringSome")
    public void exportEngineeringSome(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "ids") String ids) {
        try {
            List<EngineeringExportVo> engineeringExportVoList = 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<EngineeringVo> engineeringLists =  Mapper.getEngineeringSomeLists(num);
            for (int i = 0; i < engineeringLists.size(); i++) {
                EngineeringExportVo exportVo = new EngineeringExportVo();
                EngineeringVo vo = engineeringLists.get(i);
                BeanUtils.copyProperties(vo, exportVo);
                exportVo.setId((i + 1) + "");
                engineeringExportVoList.add(exportVo);
            }
            ExportParams exportParams = new ExportParams("工程", "工程", ExcelType.XSSF);
            exportParams.setStyle(ExcelStyleUtil.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, EngineeringExportVo.class, engineeringExportVoList);
            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);
        }
    }

    @GetMapping("exportGtSome")
    public void exportGtSome(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "ids") String ids, @RequestParam(value = "proId") String proId ) {
        try {
            List<GtExportVo> gtExportVoList = 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]);
            }
            int pro = Integer.parseInt(proId);
            List<GtVo> gtLists = Mapper.getExportGtSomeLists(num,pro);
            for (int i = 0; i < gtLists.size(); i++) {
                GtExportVo exportVo = new GtExportVo();
                GtVo vo = gtLists.get(i);
                BeanUtils.copyProperties(vo, exportVo);
                exportVo.setId((i + 1) + "");
                gtExportVoList.add(exportVo);
            }
            ExportParams exportParams = new ExportParams("杆塔", "杆塔", ExcelType.XSSF);
            exportParams.setStyle(ExcelStyleUtil.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, GtExportVo.class, gtExportVoList);
            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);
        }
    }

}
