package com.ho.business.controller;

import com.ho.business.constant.DeviceTypeConstant;
import com.ho.business.entity.RepairFile;
import com.ho.business.entity.StationNotepad;
import com.ho.business.feignclient.UserFeignClient;
import com.ho.business.service.StationNoteAndRepairService;
import com.ho.business.vo.req.StationNotepadReq;
import com.ho.business.vo.req.StationRepairReqVo;
import com.ho.business.vo.resp.StationRepairRespVo;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.entity.SimpleUser;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.service.RedisService;
import com.ho.common.tools.util.EasyExcelUtil;
import com.ho.common.tools.util.PageResult;
import com.ho.user.api.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wp
 * @desc: 电站备忘录及维修记录
 * @DateTime: 2024/10/11 10:57
 */
@RequestMapping(ContextConstant.BUSINESS + "noteAndRepair")
@RestController
@Api(tags = "业务模块-电站备忘录及维修记录")
public class StationNoteAndRepairController {

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private RedisService redisService;

    @Autowired
    private StationNoteAndRepairService stationNoteAndRepairService;

    @PostMapping("addStationNote")
    @ApiOperation("新增电站备忘录数据")
    public DataResult addStationNote(@RequestBody StationNotepad stationNotepad){
        //获取创建人名称
        SysUser userInfo = userFeignClient.getUserById(stationNotepad.getCreateUser()).getData();
        if(userInfo == null){
            throw new BusinessException(BaseResponseCode.ROLE_INFO_NOT_FOUND);
        }else{
            stationNotepad.setCreateUserName(userInfo.getRealName());
        }
        stationNoteAndRepairService.addStationNote(stationNotepad);
        return  DataResult.success();
    }

    @PostMapping("deleteStationNote")
    @ApiOperation("删除电站备忘录数据")
    public DataResult deleteStationNote(@RequestBody List<Integer> noteIds){
        if(noteIds.size() ==0){
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        stationNoteAndRepairService.deleteStationNote(noteIds.get(0));
        return  DataResult.success();
    }

    @PostMapping("updateStationNote")
    @ApiOperation("更新电站备忘录数据")
    public DataResult updateStationNote(@RequestBody StationNotepad stationNotepad){
        if(stationNotepad.getId()==null || stationNotepad.getUpdateUser() == null){
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        //获取创建人名称
        SysUser userInfo = userFeignClient.getUserById(stationNotepad.getUpdateUser()).getData();
        if(userInfo == null){
            throw new BusinessException(BaseResponseCode.ROLE_INFO_NOT_FOUND);
        }else{
            stationNotepad.setUpdateUserName(userInfo.getRealName());
        }
        stationNoteAndRepairService.updateStationNote(stationNotepad);
        return  DataResult.success();
    }

    @PostMapping("selectStationNote")
    @ApiOperation("查询电站备忘录数据")
    public DataResult selectStationNote(@RequestBody StationNotepadReq stationNotepadReq){
        List<StationNotepad> stationNotepadList =  stationNoteAndRepairService.selectStationNote(stationNotepadReq);
        if(stationNotepadReq.getPageSize() != null && stationNotepadReq.getPageNum() != null){
            //分页处理
            Integer totalNum = stationNotepadList.size();
            Integer pageSize = stationNotepadReq.getPageSize();
            Integer pageNum = stationNotepadReq.getPageNum();
            Integer totalSize = totalNum/pageSize+(totalNum%pageSize>0?1:0);
            stationNotepadList = stationNotepadList.stream().skip((pageNum-1)*pageSize).limit(pageSize).collect(Collectors.toList());
            PageResult pageResult = new PageResult();
            pageResult.setList(stationNotepadList);
            pageResult.setTotalRows(totalNum);
            pageResult.setTotalPages(totalSize);
            pageResult.setPageNum(pageNum);
            pageResult.setPageSize(pageSize);
            return DataResult.success(pageResult);
        }
        return  DataResult.success(stationNotepadList);
    }

    @PostMapping("exportStationNote")
    @ApiOperation("导出电站备忘录数据列表")
    public void exportStationNote(@RequestBody StationNotepadReq stationNotepadReq, HttpServletResponse response){
        String fileName = "电站信息";
        String sheetName = "电站信息";
        List<StationNotepad> stationNotepadList =  stationNoteAndRepairService.selectStationNote(stationNotepadReq);
        if(stationNotepadList.size()>0){
            try {
                EasyExcelUtil.writeExcel(response, stationNotepadList, fileName, sheetName, StationNotepad.class);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }


    @PostMapping("addStationRepair")
    @ApiOperation("新增电站维修记录数据")
    public DataResult addStationRepair(@RequestBody StationRepairReqVo stationRepairReqVo){
        stationNoteAndRepairService.addStationRepair(stationRepairReqVo);
        return  DataResult.success();
    }

    @PostMapping("deleteStationRepair")
    @ApiOperation("删除电站维修记录数据")
    public DataResult deleteStationRepair(@RequestBody List<Integer> repaidId,HttpServletRequest request){
        if(repaidId.size() ==0){
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByToken(token);
        String bucket = DeviceTypeConstant.GROUP + user.getGroupId();
        stationNoteAndRepairService.deleteStationRepair(repaidId.get(0),bucket);
        return  DataResult.success();
    }

    @PostMapping("updateStationRepair")
    @ApiOperation("修改电站维修记录数据")
    public DataResult updateStationRepair(@RequestBody StationRepairReqVo stationRepairReqVo){
        if(stationRepairReqVo.getId()==null ){
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        stationNoteAndRepairService.updateStationRepair(stationRepairReqVo);
        return  DataResult.success();
    }

    @PostMapping("selectStationRepair")
    @ApiOperation("查询电站维修记录数据")
    public DataResult selectStationRepair(@RequestBody StationRepairReqVo stationRepairReqVo){
        List<StationRepairRespVo> result =  stationNoteAndRepairService.selectStationRepair(stationRepairReqVo);
        if(stationRepairReqVo.getPageNum() != null && stationRepairReqVo.getPageSize() != null){
            //分页处理
            Integer totalNum = result.size();
            Integer pageSize = stationRepairReqVo.getPageSize();
            Integer pageNum = stationRepairReqVo.getPageNum();
            Integer totalSize = totalNum/pageSize+(totalNum%pageSize>0?1:0);
            result = result.stream().skip((pageNum-1)*pageSize).limit(pageSize).collect(Collectors.toList());
            PageResult pageResult = new PageResult();
            pageResult.setList(result);
            pageResult.setTotalRows(totalNum);
            pageResult.setTotalPages(totalSize);
            pageResult.setPageNum(pageNum);
            pageResult.setPageSize(pageSize);
            return DataResult.success(pageResult);
        }
        return  DataResult.success(result);
    }

    @PostMapping("exportStationRepair")
    @ApiOperation("导出电站维修记录数据列表")
    public void exportStationRepair(@RequestBody StationRepairReqVo stationRepairReqVo, HttpServletResponse response){
        String fileName = "维修记录";
        String sheetName = "维修记录";
        List<StationRepairRespVo> result =  stationNoteAndRepairService.selectStationRepair(stationRepairReqVo);
        if(result.size()>0){
            try {
                EasyExcelUtil.writeExcel(response, result, fileName, sheetName, StationRepairRespVo.class);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @PostMapping("uploadRepairFile")
    @ApiOperation("上传维修记录文件")
    public DataResult<RepairFile> uploadRepairFile(@RequestParam MultipartFile file,HttpServletRequest request){
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByToken(token);
        String bucket = DeviceTypeConstant.GROUP + user.getGroupId();
        RepairFile repairFile = stationNoteAndRepairService.uploadRepairFile(file,bucket);
        return DataResult.success(repairFile);
    }
}
