package com.nantian.cloud.jly.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jly.entity.ConfigureGaugeVerify;
import com.nantian.cloud.jly.service.ConfigureGaugeVerifyService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.minio.service.MinioTemplate;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


/**
 * 能源计量器具校准记录
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@RestController
@AllArgsConstructor
@RequestMapping("/configureGaugeVerify")
@Api(value = "configureGaugeVerify", tags = "能源计量器具校准记录")
public class ConfigureGaugeVerifyController {
	private final ConfigureGaugeVerifyService configureGaugeVerifyService;
	private final MinioTemplate minioTemplate;


	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param configureGaugeVerify 请假审批单
	 * @return
	 */
	@SysLog("能源计量器具更换记录-分页查询")
	@GetMapping("/page")
	@ApiOperation(value = "分页查询能源计量器具校准记录",notes = "分页查询能源计量器具校准记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
			@ApiImplicitParam(name = "ConfigureGaugeVerify", value = "能源计量器具校准记录", required = false, dataType = "json", paramType = "path")
	})
	public R pageCmp(Page page, ConfigureGaugeVerify configureGaugeVerify) {
		configureGaugeVerify.setDelFlag("1"); //查询未删除数据
		//return R.ok(configureGaugeVerifyService.page(page, Wrappers.query(configureGaugeVerify)));
		return R.ok(configureGaugeVerifyService.pageCmp(page, configureGaugeVerify));
	}


	/**
	 * 信息
	 *
	 * @param configureGaugeVerifyId
	 * @return R
	 */
	@SysLog("能源计量器具更换记录-根据id查询能源计量器具校准记录")
	@GetMapping("/{configureGaugeVerifyId}")
	@ApiOperation(value = "根据id查询能源计量器具校准记录",notes = "根据id查询能源计量器具校准记录")
	@ApiImplicitParam(name = "configureGaugeVerifyId", value = "能源计量器具校准记录id", required = true, dataType = "int", paramType = "path")
	public R getById(@PathVariable("configureGaugeVerifyId") Integer configureGaugeVerifyId) {
		return R.ok(configureGaugeVerifyService.getById(configureGaugeVerifyId));
	}

	/**
	 * 保存/修改能源计量器具校准记录
	 *
	 * @param configureGaugeVerify
	 * @return R
	 */
	@SysLog("能源计量器具更换记录-保存/修改能源计量器具校准记录")
	@PostMapping
	@ApiOperation(value = "保存/修改能源计量器具校准记录",notes = "保存/修改能源计量器具校准记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ConfigureGaugeVerify", value = "能源计量器具校准记录", required = true, dataType = "json", paramType = "path"),
			@ApiImplicitParam(name = "barCodeFile", value = "文件流（计量器二维码）", required = false, dataType = "File", paramType = "path"),
			@ApiImplicitParam(name = "verifyTimeStr", value = "校验时间字符串", required = false, dataType = "string", paramType = "path")
	})
	public R save( ConfigureGaugeVerify configureGaugeVerify, MultipartFile barCodeFile, String verifyTimeStr) throws Exception {
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			configureGaugeVerify.setUserid(u.getId());
		}
		if (u.getDeptId() != null) {
			configureGaugeVerify.setDeptId(u.getDeptId());
		}
		configureGaugeVerify.setEnterpriseCode(u.getEnterpriseCode());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		if (verifyTimeStr != null && !"".equals(verifyTimeStr)) {
			//configureGaugeVerify.setVerifyTime(df.parse(verifyTimeStr));
			configureGaugeVerify.setVerifyTime(new Date(verifyTimeStr)); //Thu Sep 10 2020 00:00:00 GMT+0800 (中国标准时间)
		}
//		configureGaugeVerify.setEnterpriseCode(u.getEnterpriseCode());
		configureGaugeVerify.setDelFlag("1");
		configureGaugeVerify.setUpdateTime(new Date());
		if(barCodeFile!=null){
			if(barCodeFile.getSize()>0) {
				//计量器二维码
				configureGaugeVerify.setBarCode(getFileUrl(barCodeFile));
			}
		}
		return R.ok(configureGaugeVerifyService.save(configureGaugeVerify));
	}

	@SysLog("能源计量器具更换记录-上传文件")
	public String getFileUrl(MultipartFile file) throws Exception {
		String uuid = UUID.randomUUID().toString();
		InputStream inputStream = file.getInputStream(); //得到文件流
		String fileName = file.getOriginalFilename();    //文件名（含后缀）
		//minioTemplate.putObject("jly", uuid, inputStream);
		minioTemplate.putObject("jly", uuid, inputStream,inputStream.available(),"application/octet-stream");
		return fileName + "<" + uuid;
	}

	/**
	 * 修改
	 *
	 * @param configureGaugeVerify
	 * @return R
	 */
	@SysLog("能源计量器具更换记录-修改")
	@PutMapping
	@ApiOperation(value = "修改能源计量器具校准记录",notes = "修改能源计量器具校准记录")
	@ApiImplicitParam(name = "ConfigureGaugeVerify", value = "能源计量器具校准记录", required = true, dataType = "json", paramType = "path")
	public R updateById(@RequestBody ConfigureGaugeVerify configureGaugeVerify) {
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			configureGaugeVerify.setUserid(u.getId());
		}
		configureGaugeVerify.setUpdateTime(new Date());
		return R.ok(configureGaugeVerifyService.updateGaugeVerify(configureGaugeVerify));
	}

	/**
	 * 删除
	 *
	 * @param configureGaugeVerifyId
	 * @return R
	 */
	@SysLog("能源计量器具更换记录-删除")
	@DeleteMapping("/{configureGaugeVerifyId}")
	@ApiOperation(value = "根据id删除能源计量器具校准记录",notes = "根据id删除能源计量器具校准记录")
	@ApiImplicitParam(name = "configureGaugeVerifyId", value = "能源计量器具校准记录id", required = true, dataType = "int", paramType = "path")
	public R removeById(@PathVariable("configureGaugeVerifyId") Long configureGaugeVerifyId) {
		ConfigureGaugeVerify configureGaugeVerify = configureGaugeVerifyService.getById(configureGaugeVerifyId);
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			configureGaugeVerify.setUserid(u.getId());
		}
		configureGaugeVerify.setDelFlag("4");
		configureGaugeVerify.setUpdateTime(new Date());
		return R.ok(configureGaugeVerifyService.updateById(configureGaugeVerify));
	}

	/**
	 * 分页查询计量器具更换记录
	 *
	 * @return R
	 */
	@SysLog("能源计量器具更换记录-分页查询计量器具更换记录")
	@ApiOperation(value = "分页查询计量器具更换记录",notes = "分页查询计量器具更换记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "Page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
			@ApiImplicitParam(name = "areaCodes", value = "地区编码", required = false, dataType = "string", paramType = "path"),
			@ApiImplicitParam(name = "industryCode", value = "行业编码", required = false, dataType = "string", paramType = "path"),
			@ApiImplicitParam(name = "cmpName", value = "企业名称", required = false, dataType = "string", paramType = "path"),
			@ApiImplicitParam(name = "cmp", value = "企业编码", required = false, dataType = "string", paramType = "path"),
	})
	@GetMapping("/getPageData")
	public R getPageData(Page page, String areaCodes, String industryCode, String cmpName, String cmp) {
		return R.ok(configureGaugeVerifyService.getPageData(page,areaCodes,industryCode,cmpName,cmp));
	}

}
