package com.mosty.base.manager.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mosty.base.manager.service.TpjcglZnzbService;
import com.mosty.base.model.dto.jcgl.TbJcglZnzbSearchDto;
import com.mosty.base.model.dto.qwzx.AppSearchQuery;
import com.mosty.base.model.entity.jcgl.TbJcglZnzb;
import com.mosty.base.model.query.jcgl.TbJcglZnzbQuery;
import com.mosty.base.model.query.jcgl.TbJcglZyzbQuery;
import com.mosty.base.model.vo.jcgl.TbJcglZnzbVo;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.base.entity.log.BusinessType;
import com.mosty.common.base.entity.log.Log;
import com.mosty.common.config.Excel.ExcelUtil;
import com.mosty.common.token.JwtSysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@AllArgsConstructor
@RequestMapping({"/tpjcglZnzb"})
@Api(tags = {"智能装备管理"})
public class TpjcglZnzbController {

    private final TpjcglZnzbService tpjcglZnzbService;


    @PostMapping({"/xfZnzb"})
    @JwtSysUser
    @Log(title = "下发智能", businessType = BusinessType.INSERT)
    @ApiOperation(value = "下发智能")
    public ResponseResult xfZnzb(@RequestBody TbJcglZnzb zb) {
        tpjcglZnzbService.xfZnzb(zb);
        return ResponseResult.success();

    }

    @GetMapping({"/htZnzb"})
    @JwtSysUser
    @ApiOperation(value = "回退智能装备")
    public ResponseResult htZnzb(String id) {
        tpjcglZnzbService.htZnzb(id);
        return ResponseResult.success();
    }

    @GetMapping({"/bsZnzb"})
    @JwtSysUser
    @ApiOperation(value = "报损智能装备")
    public ResponseResult bsZnzb(String id) {
        tpjcglZnzbService.bsZnzb(id);
        return ResponseResult.success();
    }


    @PostMapping({"/addZnzb"})
    @JwtSysUser
    @Log(title = "新增智能装备", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增智能装备")
    public ResponseResult<Integer> addZnzb(@RequestBody TbJcglZnzb tbJcglZnzb) {
        return ResponseResult.success(tpjcglZnzbService.addZnzb(tbJcglZnzb));
    }


    @PostMapping({"/getZyzb"})
    @JwtSysUser
    @ApiOperation(value = "查询专业装备")
    public ResponseResult<List<TbJcglZnzb>> getZyzb(@RequestBody TbJcglZyzbQuery tbJcglZyzbQuery) {
        return ResponseResult.success(tpjcglZnzbService.getZyzb(tbJcglZyzbQuery));
    }

    @GetMapping({"/getZnzb"})
    @JwtSysUser
    @ApiOperation(value = "查询智能装备")
    public ResponseResult<IPage<TbJcglZnzbVo>> getZnzb(TbJcglZnzb zb) {
        IPage<TbJcglZnzb> tbJcglZnzbIPage = new Page<>(zb.getPageNo(), zb.getPageSize());
        return ResponseResult.success(tpjcglZnzbService.getZnzb(zb, tbJcglZnzbIPage));
    }

    @PostMapping({"/editZnzb"})
    @JwtSysUser
    @Log(title = "编辑智能装备", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "编辑智能装备")
    public ResponseResult editZnzb(@RequestBody TbJcglZnzb tbJcglZnzb) {
        tpjcglZnzbService.editZnzb(tbJcglZnzb);
        return ResponseResult.success();
    }

    @PostMapping({"/deleteZnzb"})
    @JwtSysUser
    @Log(title = "删除智能装备", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除智能装备")
    public ResponseResult deleteZnzb(@RequestBody List<String> id) {
        tpjcglZnzbService.deleteZnzb(id);
        return ResponseResult.success();
    }

    @GetMapping("selectZbxq")
    @JwtSysUser
    @ApiOperation(value = "id查询智能装备")
    public ResponseResult<TbJcglZnzbVo> selectZbxq(String id) {
        return ResponseResult.success(tpjcglZnzbService.selectZbxq(id));
    }

    @GetMapping({"/znzbDqtx"})
    @JwtSysUser
    @ApiOperation(value = "智能装备到期提醒")
    public ResponseResult<IPage<TbJcglZnzb>> znzbDqtx(TbJcglZnzbQuery tbJcglZnzb) {
        IPage<TbJcglZnzb> tbJcglZnzbIPage = new Page<>(tbJcglZnzb.getPageCurrent(), tbJcglZnzb.getPageSize());
        return ResponseResult.success(tpjcglZnzbService.znzbDqtx(tbJcglZnzb, tbJcglZnzbIPage));
    }

    @GetMapping({"/checkZnzb"})
    @JwtSysUser
    @ApiOperation(value = "查询装备到期和故障，提醒部门下的所有人")
    public ResponseResult<Object> checkZnzb() {
        return ResponseResult.success(this.tpjcglZnzbService.checkZnzb());
    }

    @GetMapping({"/getFjzbList"})
    @JwtSysUser
    @ApiOperation(value = "查询附近的智能装备")
    public ResponseResult<IPage<TbJcglZnzbVo>> getFjzbList(TbJcglZnzbSearchDto dto) {
        return ResponseResult.success(this.tpjcglZnzbService.getFjzbList(dto));
    }

    @GetMapping("/getZnzbList")
    @JwtSysUser
    @ApiOperation(value = "app获取智能装备信息")
    public ResponseResult<IPage<TbJcglZnzbVo>> getZnzbList(AppSearchQuery dto) {
        return ResponseResult.success(this.tpjcglZnzbService.getZnzbList(dto));
    }

    @GetMapping({"/zyZbtj"})
    @JwtSysUser
    @ApiOperation(value = "资源装备统计")
    public ResponseResult<Map<String, Object>> zyZbtj(String ssbmdm) {
        return ResponseResult.success(tpjcglZnzbService.zyZbtj(ssbmdm));
    }

    @GetMapping({"/sblxTj"})
    @JwtSysUser
    @ApiOperation(value = "设备类型统计")
    public ResponseResult<List<Map<String, Object>>> sblxTj(String type, String ssbmdm) {
        return ResponseResult.success(tpjcglZnzbService.sblxTj(type, ssbmdm));
    }

    @Log(title = "导入智能装备", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @JwtSysUser
    public ResponseResult<String> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<TbJcglZnzb> util = new ExcelUtil<TbJcglZnzb>(TbJcglZnzb.class);
        List<TbJcglZnzb> userList = util.importExcel(file.getInputStream());
        String s = this.tpjcglZnzbService.importData(userList, updateSupport);
        return ResponseResult.success(s);
    }


    @Log(title = "模板下载", businessType = BusinessType.IMPORT)
    @GetMapping("/importTemplate")
    @JwtSysUser
    public void importTemplate(HttpServletResponse response) throws Exception {
        ExcelUtil<TbJcglZnzb> util = new ExcelUtil<TbJcglZnzb>(TbJcglZnzb.class);
        util.importTemplateExcel(response, "智能装备");
    }

}
