package com.hssx.pcbms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hssx.pcbms.entity.*;
import com.hssx.pcbms.entity.vo.GoodsVO;
import com.hssx.pcbms.entity.vo.MaintainRecordVO;
import com.hssx.pcbms.service.GoodsService;
import com.hssx.pcbms.util.HttpRespMsg;
import com.hssx.pcbms.util.PageUtil;
import com.hssx.pcbms.util.UploadFileToFileNameUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @author 吴涛涛
 * @since 2019-10-25
 */
@Controller
@RequestMapping("/goods")
@EnableScheduling
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @Value("${upload.path}")
    private String path;

    /**
     * 资产录入
     * 参数：
     * userId :当前登录人的id
     * name ：名称，tagId：标签id,modelNumber：模具型号，company:公司
     * ，address：地址，factory：生产厂家，pic：产品图片文件路径，serviceLife：服务期限（天）,
     * nextIndate:下次维护日期，number:数量,deptStr:部门编号前缀,goodStr:产品编号前缀
     * 注：产品编号=部门编号前缀+产品编号前缀+随机数
     *
     * @return
     */
    @ApiOperation(value = "资产录入", notes = "资产录入方法")
    @RequestMapping("/add")
    @ResponseBody
    public HttpRespMsg add(GoodsVO goodsVO) {
        HttpRespMsg msg = goodsService.add(goodsVO);
        return msg;
    }

    /**
     * 图片上传通用接口
     * 参数：
     * file：产品图片文件
     *
     * @return
     */
    @ApiOperation(value = "图片上传通用接口", notes = "图片上传通用接口方法")
    @RequestMapping("/uploadFile")
    @ResponseBody
    public HttpRespMsg uploadFile(MultipartFile file) {
        HttpRespMsg msg = new HttpRespMsg();
        String filePath = "";
        if (file != null) {
            filePath = UploadFileToFileNameUtil.uploadFile(file, path);
        }
        if(filePath.equals("")){
            msg.setError("上传图片失败了");
        }
        msg.data = filePath;
        return msg;
    }

    /**
     * 资产基本信息修改
     * 参数：id:资产信息id
     * name ：名称，tagId：标签id,modelNumber：模具型号，company:公司
     * ，address：地址，factory：生产厂家，pic：产品图片文件路径，serviceLife：服务期限（天）,
     * nextIndate:下次维护日期（以上信息除了id外未修改的均不传）
     *
     * @return
     */
    @ApiOperation(value = "资产基本信息修改", notes = "资产基本信息修改方法")
    @RequestMapping("/update")
    @ResponseBody
    public HttpRespMsg updateInfo(Goods goods, @RequestParam(required = false) MultipartFile file) {
        HttpRespMsg msg = goodsService.updateInfo(goods, file);
        return msg;
    }


    /**
     * 资产列表
     * 参数：
     * name ：名称，keyName:关键字,pageNum:当前页，pageSize：每页多少条
     * tagId：标签id(单个标签)（查看全部不传）-1（机柜） -2（设备）
     * @return
     */
    @ApiOperation(value = "资产列表", notes = "资产列表方法")
    @RequestMapping("/list")
    @ResponseBody
    public HttpRespMsg list(PageUtil page, @RequestParam(required = false) String keyName,
                            @RequestParam(required = false)Integer tagId,
                            @RequestParam(required = false)Integer uid) {
        HttpRespMsg msg = goodsService.getList(page, keyName, tagId,uid);
        return msg;
    }
    /**
     * 资产列表Excel导出
     * 参数：
     * name ：名称，keyName:关键字
     * tagId：标签id(单个标签)（查看全部不传）
     * @return
     */
    @ApiOperation(value = "资产列表", notes = "资产列表方法")
    @RequestMapping("/listExcel")
    @ResponseBody
    public HttpRespMsg listExcel(PageUtil page, @RequestParam(required = false) String keyName,
                            @RequestParam(required = false)Integer tagId,
                            @RequestParam(required = false)Integer uid,HttpServletResponse response) {
        HttpRespMsg msg = goodsService.listExcel(page, keyName, tagId,uid,response);
        return msg;
    }
    /**
     * 我的资产列表
     * 参数：
     * name ：名称，keyName:关键字,pageNum:当前页，pageSize：每页多少条
     * uid:所属人id
     * @return
     */
    @ApiOperation(value = "我的资产列表", notes = "我的资产列表方法")
    @RequestMapping("/myList")
    @ResponseBody
    public HttpRespMsg myList(PageUtil page, @RequestParam(required = false) String keyName,
                            @RequestParam(required = false)Integer tagId,
                            @RequestParam(required = false)Integer uid) {
        HttpRespMsg msg = goodsService.myList(page, keyName,uid);
        return msg;
    }

    /**
     * 资产详情
     * 参数：id:资产id
     *
     * @return
     */
    @ApiOperation(value = "资产详情", notes = "资产详情方法")
    @RequestMapping("/detail")
    @ResponseBody
    public HttpRespMsg detail(Integer id) {
        HttpRespMsg msg = goodsService.getDetail(id);
        return msg;
    }

    /**
     * 分配资产
     * 参数：goodsId:资产id ，divisionCode 部门编号，suffixCode 字母前缀
     *
     * @return
     */
    @ApiOperation(value = "分配资产", notes = "分配资产方法")
    @RequestMapping("/distribute")
    @ResponseBody
    public HttpRespMsg distribute(GoodsNo goodsNo) {
        HttpRespMsg msg = goodsService.distribute(goodsNo);
        return msg;
    }

    /**
     * 资产基本信息的删除
     * 参数：id：资产基本信息id
     *
     * @return
     */
    @ApiOperation(value = "资产基本信息的删除", notes = "资产的删除方法")
    @RequestMapping("/del")
    @ResponseBody
    public HttpRespMsg del(Goods goods) {
        HttpRespMsg msg = goodsService.del(goods);
        return msg;
    }

    /**
     * 资产编号的删除
     * 参数：id：资产编号id
     *
     * @return
     */
    @ApiOperation(value = "资产的删除", notes = "资产的删除方法")
    @RequestMapping("/delNo")
    @ResponseBody
    public HttpRespMsg delNo(GoodsNo goodsNo) {
        HttpRespMsg msg = goodsService.delNo(goodsNo);
        return msg;
    }

    /**
     * 处置资产
     * 参数：
     * id:对应编号的id,state:资产状态0-空闲 1-在用 3-报废
     * userId :对应人员的id(不选传0),currentUserId：当前人id
     *
     * @return
     */
    @ApiOperation(value = "处置资产", notes = "处置资产方法")
    @RequestMapping("/handle")
    @ResponseBody
    public HttpRespMsg handle(GoodsNo goodsNo, Integer currentUserId) {
        HttpRespMsg msg = goodsService.handle(goodsNo, currentUserId);
        return msg;
    }

    /**
     * 资产编号详细
     * 参数：
     * id:对应编号的id
     *
     * @return
     */
    @ApiOperation(value = "资产编号详细", notes = "资产编号详细方法")
    @RequestMapping("/goodsNoDetail")
    @ResponseBody
    public HttpRespMsg goodsNoDetail(GoodsNo goodsNo) {
        HttpRespMsg msg = goodsService.goodsNoDetailById(goodsNo);
        return msg;
    }

    /**
     * 资产编号添加
     * 参数：
     * goodsId:资产的goodsId，count:数量
     *
     * @return
     */
    @ApiOperation(value = "资产编号添加", notes = "资产编号添加方法")
    @RequestMapping("/goodsNoAdd")
    @ResponseBody
    public HttpRespMsg goodsNoAdd(GoodsNo goodsNo, Integer count) {
        HttpRespMsg msg = goodsService.goodsNoAdd(goodsNo, count);
        return msg;
    }

    /**
     * 处置资产操作记录
     * 参数：
     * id:资产id,pageNum:当前页，pageSize：每页多少条
     *
     * @return
     */
    @ApiOperation(value = "处置资产操作记录", notes = "处置资产操作记录方法")
    @RequestMapping("/operateList")
    @ResponseBody
    public HttpRespMsg operateList(Goods goods,PageUtil page) {
        HttpRespMsg msg = goodsService.operateList(goods,page);
        return msg;
    }
    /**
     * 维护资产
     * 参数：
     * goodId:资产id
     * ids:编号对应id的字符串 如:"1,2,3", operator 维护人名字，operatorPhone 维护人联系方式
     * company 维护公司名字,companyPhone 维护公司联系方式
     * @return
     */
    @ApiOperation(value = "维护资产", notes = "维护资产方法")
    @RequestMapping("/maintain")
    @ResponseBody
    public HttpRespMsg maintain(MaintainRecordVO maintainRecordVO) {
        HttpRespMsg msg = goodsService.maintain(maintainRecordVO);
        return msg;
    }
    /**
     * 待维护资产列表
     * 参数：
     * id:资产id
     *
     * @return
     */
    @ApiOperation(value = "待维护资产列表", notes = "待维护资产列表方法")
    @RequestMapping("/isMaintainList")
    @ResponseBody
    public HttpRespMsg isMaintainList(Goods goods) {
        HttpRespMsg msg = goodsService.isMaintainList(goods);
        return msg;
    }
    /**
     * 维护资产记录
     * 参数：
     * id:资产id,pageNum:当前页，pageSize：每页多少条
     *
     * @return
     */
    @ApiOperation(value = "维护资产记录", notes = "维护资产记录方法")
    @RequestMapping("/isMaintainRecord")
    @ResponseBody
    public HttpRespMsg isMaintainRecord(Goods goods,PageUtil page) {
        HttpRespMsg msg = goodsService.isMaintainRecord(goods,page);
        return msg;
    }
    /**
     * 处置资产记录的excel导出
     * 参数：
     * id:资产id
     *
     * @return
     */
    @ApiOperation(value = "处置资产记录的excel导出", notes = "处置资产记录的excel导出方法")
    @RequestMapping("/handelRecordExcel")
    @ResponseBody
    public HttpRespMsg handelRecordExcel(Goods goods,HttpServletResponse response) {
        HttpRespMsg msg = goodsService.handelRecordExcel(goods,response);
        return msg;
    }
    /**
     * 保养资产记录的excel导出
     * 参数：
     * id:资产id
     *
     * @return
     */
    @ApiOperation(value = "保养资产记录的excel导出", notes = "保养资产记录的excel导出方法")
    @RequestMapping("/isMaintainRecordExcel")
    @ResponseBody
    public HttpRespMsg isMaintainExcel(Goods goods,HttpServletResponse response) {
        HttpRespMsg msg = goodsService.isMaintainExcel(goods,response);
        return msg;
    }
    /**
     * 资产维护检测定时任务
     * 参数：
     * @return
     */
    @ApiOperation(value = "资产维护检测定时任务", notes = "资产维护检测定时任务")
    @RequestMapping("/goodsMaintainChecking")
    @ResponseBody
    @Scheduled(cron = "0 0 23 * * ?")//配置时间点触发(每日23点)
    public HttpRespMsg goodsMaintainChecking() {
        HttpRespMsg msg = goodsService.goodsMaintainChecking();
        return msg;
    }


    /**
     * 给设备上下架的机架列表
     * tagId:-1
     * 参数：
     * @return
     */
    @ApiOperation(value = "给设备上下架的机架列表", notes = "给设备上下架的机架列表")
    @RequestMapping("/rackList")
    @ResponseBody
    public HttpRespMsg getRackList(Goods goods) {
        HttpRespMsg msg = new HttpRespMsg();
        msg.data = goodsService.list(new QueryWrapper<Goods>().eq("good_type",1).eq("tag_id",-1).select("id","name","rack_id").orderByDesc("indate"));
        return msg;
    }




}

