package org.jeecg.modules.device.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.base.entity.BuSupplier;
import org.jeecg.modules.base.service.IBuSupplierService;
import org.jeecg.modules.device.entity.BuDevice;
import org.jeecg.modules.device.entity.BuDeviceLifeCircle;
import org.jeecg.modules.device.service.IBuDeviceLifeCircleService;
import org.jeecg.modules.device.service.IBuDeviceService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

 /**
 * @Description: 设备生命周期表
 * @Author: jeecg-boot
 * @Date:   2020-12-11
 * @Version: V1.0
 */
@Api(tags="设备生命周期表")
@RestController
@RequestMapping("/device/buDeviceLifeCircle")
@Slf4j
public class BuDeviceLifeCircleController extends JeecgController<BuDeviceLifeCircle, IBuDeviceLifeCircleService> {
	@Autowired
	private IBuDeviceLifeCircleService buDeviceLifeCircleService;
	@Autowired
	private IBuDeviceService buDeviceService;
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private IBuSupplierService buSupplierService;



	/**
	 * 分页列表查询
	 *
	 * @param buDeviceLifeCircle;
	 * @param pageNo;
	 * @param pageSize;
	 * @param req;
	 * @return
	 */
	@AutoLog(value = "设备生命周期表-分页列表查询")
	@ApiOperation(value="设备生命周期表-分页列表查询", notes="设备生命周期表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(BuDeviceLifeCircle buDeviceLifeCircle,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BuDeviceLifeCircle> queryWrapper = QueryGenerator.initQueryWrapper(buDeviceLifeCircle, req.getParameterMap());
		Page<BuDeviceLifeCircle> page = new Page<BuDeviceLifeCircle>(pageNo, pageSize);
		IPage<BuDeviceLifeCircle> pageList = buDeviceLifeCircleService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	/**
	 *   添加
	 *
	 */
	@AutoLog(value = "设备生命周期表-添加")
	@ApiOperation(value="设备生命周期表-添加", notes="设备生命周期表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BuDeviceLifeCircle buDeviceLifeCircle) {
		buDeviceLifeCircleService.save(buDeviceLifeCircle);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 */
	@AutoLog(value = "设备生命周期表-编辑")
	@ApiOperation(value="设备生命周期表-编辑", notes="设备生命周期表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BuDeviceLifeCircle buDeviceLifeCircle) {
		buDeviceLifeCircleService.updateById(buDeviceLifeCircle);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "设备生命周期表-通过id删除")
	@ApiOperation(value="设备生命周期表-通过id删除", notes="设备生命周期表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		buDeviceLifeCircleService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "设备生命周期表-批量删除")
	@ApiOperation(value="设备生命周期表-批量删除", notes="设备生命周期表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.buDeviceLifeCircleService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "设备生命周期表-通过id查询")
	@ApiOperation(value="设备生命周期表-通过id查询", notes="设备生命周期表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		BuDeviceLifeCircle buDeviceLifeCircle = buDeviceLifeCircleService.getById(id);
		if(buDeviceLifeCircle==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(buDeviceLifeCircle);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param buDeviceLifeCircle
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BuDeviceLifeCircle buDeviceLifeCircle) {
        return super.exportXls(request, buDeviceLifeCircle, BuDeviceLifeCircle.class, "设备生命周期表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BuDeviceLifeCircle.class);
    }
	//  获取设备维保和维修次数信息
	 @GetMapping(value = "/getTimeByDeviceId")
	 public Result<?> getTimeByDeviceId(@RequestParam(name="deviceId", required = true) String deviceId) throws ParseException {
		 BuDeviceLifeCircle buDeviceLifeCircle = buDeviceLifeCircleService.getDateByDeviceId(deviceId);
//		 int times = buRepairRecordService.getTimesById(deviceId);// 维修次数
		 String startTime = buDeviceLifeCircleService.getTimeByDeviceId(deviceId);// 启封日期
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 Date nowDate = new Date();
		 Date oldDate = sdf.parse(startTime);
		 int day = (int)((nowDate.getTime() - oldDate.getTime())/1000/60/60/24);
		 buDeviceLifeCircle.setBuyDes(Integer.toString(day));
		 return Result.OK(buDeviceLifeCircle);
	 }
	// 获取设备基本信息
	 @GetMapping(value = "/getDeviceDataById")
	 public Result<?> getDeviceDataById(@RequestParam(name="deviceId", required = true) String deviceId) {
		 BuDevice buDevice = buDeviceService.getById(deviceId);
		 SysUser sysUser = sysUserService.getUserByName(buDevice.getDirectorId());
		 BuSupplier buSupplier = buSupplierService.getById(buDevice.getSupplierId());
		 buDevice.setSupplierId(buSupplier.getName());
		 buDevice.setDirectorId(sysUser.getRealname());
		 return Result.OK(buDevice);
	 }



}
