package com.casic.missiles.controller.meter;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.core.base.controller.ExportController;
import com.casic.missiles.core.page.PageFactory;
import com.casic.missiles.core.page.PageInfoBT;
import com.casic.missiles.dto.IdDTO;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.meter.MeterStaffImport;
import com.casic.missiles.dto.meter.MeterStaffRequest;
import com.casic.missiles.model.meter.MeterStaff;
import com.casic.missiles.model.meter.MeterTrainStaff;
import com.casic.missiles.model.system.UserInfo;
import com.casic.missiles.service.listeners.CommonExcelListener;
import com.casic.missiles.service.meter.MeterStaffService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 计量管理-计量人员-计量人员表 前端控制器
 * </p>
 *
 */
@Slf4j
@Api(tags = "计量人员表接口")
@RestController
@RequestMapping("/staff")
public class MeterStaffController extends ExportController {

    @Resource
    private MeterStaffService meterStaffService;

    /**
     * 查询计量人员列表
     * @return
     */
    @ApiOperation("计量人员列表")
    @PostMapping("/selectStaffList")
    @ResponseBody
    public ReturnDTO<Page<MeterStaff>> listPage(@RequestBody MeterStaffRequest meterStaffRequest) {
        Page<MeterStaff> page = PageFactory.defaultPage();
        Page<MeterStaff> list = meterStaffService.selectStaffList(page,meterStaffRequest);
        return ReturnUtil.success(list);
    }

    /**
     * 计量人员新增
     * @return
     */
    @ApiOperation("计量人员新增")
    @PostMapping("/addStaffInfo")
    @ResponseBody
    public ReturnDTO addStaffInfo(@RequestBody MeterStaff meterStaff) {
        return meterStaffService.addStaffInfo(meterStaff);
    }

    /**
     * 计量人员编辑
     * @return
     */
    @ApiOperation("计量人员编辑")
    @PostMapping("/updateStaffInfo")
    @ResponseBody
    public ReturnDTO updateStaffInfo(@RequestBody MeterStaff meterStaff) {
        return meterStaffService.updateStaffInfo(meterStaff);
    }

    /**
     * 人员详细信息
     */
    @ApiOperation("人员详细信息")
    @PostMapping("/selectStaffInfo")
    @ResponseBody
    public ReturnDTO<MeterStaff> selectStaffInfo(@RequestBody IdDTO idDto) {
        MeterStaff result = meterStaffService.selectStaffInfo(idDto);
        return ReturnUtil.success(result);
    }

    /**
     * 计量人员删除
     */
    @ApiOperation("计量人员删除")
    @PostMapping("/deleteStaff")
    @ResponseBody
    public ReturnDTO deleteStaff(@RequestBody IdDTO idDto) {
        return meterStaffService.deleteStaff(idDto);
    }

    /**
     * 选择人员列表查询
     */
    @ApiOperation("选择人员列表查询")
    @GetMapping("/getUserList")
    @ResponseBody
    public Object getUserList(String name,String deptId) {
        Page<UserInfo> page = PageFactory.defaultPage();
        Page<UserInfo> list = meterStaffService.getUserList(name,deptId,page);
        return ReturnUtil.success(list);
    }

    /**
     * 计量人员列表导出
     */
    @ApiOperation("计量人员列表导出")
    @PostMapping("/exportStaffList")
    @ResponseBody
    public void exportStaffList(@RequestBody MeterStaffRequest meterStaffRequest, HttpServletResponse response) {
        try {
            meterStaffService.exportStaffList(meterStaffRequest,response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @ApiOperation("批量导入人员信息")
    @PostMapping(value = "/batchImportStaff", headers = "content-type=multipart/form-data")
    @ResponseBody
    public ReturnDTO batchImportStaff(@RequestParam MultipartFile[] multipartFile) {
        try {
            CommonExcelListener<MeterStaffImport> listener = new CommonExcelListener<>();
            final long start = System.currentTimeMillis();
            for (MultipartFile file : multipartFile) {
                EasyExcel.read(file.getInputStream(), MeterStaffImport.class, listener)
                        .headRowNumber(1).autoTrim(true).sheet(0).doRead();
            }
            log.info("耗时：秒" + (System.currentTimeMillis() - start) / 1000);
            //读取到的数据
            List<MeterStaffImport> list = listener.getList();
            return meterStaffService.dataCheck(list);
        } catch (IOException e) {
            e.printStackTrace();
            return ReturnUtil.failed("数据获取失败");
        }
    }

    /**
     * 通过部门查询人员列表
     * @return
     */
    @ApiOperation("通过部门查询人员列表")
    @PostMapping("/selectStaffListByDept")
    @ResponseBody
    public ReturnDTO<List<MeterStaff>> selectStaffListByDept(@RequestBody MeterStaffRequest meterStaffRequest) {
        List<MeterStaff> list = meterStaffService.selectStaffListByDept(meterStaffRequest);
        return ReturnUtil.success(list);
    }

    /**
     * 计量工作培训记录
     * @return
     */
    @ApiOperation("计量工作培训记录")
    @PostMapping("/staffCultivateLog")
    @ResponseBody
    public ReturnDTO<PageInfoBT<MeterTrainStaff>> staffCultivateLog(@RequestBody IdDTO idDto) {
        Page<MeterTrainStaff> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(meterStaffService.staffCultivateLog(page,idDto)));
    }

    /**
     * 计量工作培训记录编辑
     * @return
     */
    @ApiOperation("计量工作培训记录编辑")
    @PostMapping("/updateStaffCultivateLog")
    @ResponseBody
    public ReturnDTO updateStaffCultivateLog(@RequestBody List<MeterTrainStaff> list) {
        return meterStaffService.updateStaffCultivateLog(list);
    }

    /**
     * 是否计量人员
     */
    @ApiOperation("是否计量人员")
    @GetMapping("/isMeterStaff")
    @ResponseBody
    public ReturnDTO<Map<String, Integer>> isMeterStaff() {
        return meterStaffService.isMeterStaff();
    }

}
