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.ConfigureGaugeReplace;
import com.nantian.cloud.jly.service.ConfigureGaugeReplaceService;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/configureGaugeReplace")
@Api(value = "configureGaugeReplace",tags = "能源计量器具更换记录")
public class ConfigureGaugeReplaceController {
	private final ConfigureGaugeReplaceService configureGaugeReplaceService;
	private final MinioTemplate minioTemplate;


	/**
	 * 分页查询
	 * @return
	 */
	@SysLog("能源计量器具更换记录-分页查询")
	@ApiOperation(value = "查询能源计量器具更换记录",notes = "条件查询能源计量器具更换记录")
	@ApiImplicitParams({
			@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"),
			@ApiImplicitParam(name ="Page",value = "分页对象",required = false,dataType = "json",paramType = "path"),
			@ApiImplicitParam(name ="installManageNo",value = "新装计量器出产序号",required = false,dataType = "string",paramType = "path"),
			@ApiImplicitParam(name ="replacedManageNo",value = "被替换计量器出产序号",required = false,dataType = "json",paramType = "path")
	})
	@GetMapping("/getPageData")
	public R getConfigureGaugeReplacePage(Page page, String areaCodes, String industryCode, String cmpName, String cmp, String installManageNo, String replacedManageNo) {
		//configureGaugeReplace.setDelFlag("1"); //查询未删除数据
		return R.ok(configureGaugeReplaceService.getPageData(page, areaCodes, industryCode, cmpName, cmp,installManageNo,replacedManageNo));
		//return R.ok(configureGaugeReplaceService.getPageData(page, Wrappers.query(configureGaugeReplace)));
	}


	/**
	 * 分页查询
	 * @return
	 */
	@SysLog("能源计量器具更换记录-分页查询")
	@ApiOperation(value = "查询能源计量器具更换记录",notes = "条件查询能源计量器具更换记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name ="Page",value = "分页对象",required = false,dataType = "json",paramType = "path"),
			@ApiImplicitParam(name ="ConfigureGaugeReplace",value = "能源计量器具",required = false,dataType = "string",paramType = "path")
	})
	@GetMapping("/pageCmp")
	public R pageCmp(Page page, ConfigureGaugeReplace configureGaugeReplace) {
		ScUser u= SecurityUtils.getUser();
		configureGaugeReplace.setEnterpriseCode(u.getEnterpriseCode());
		configureGaugeReplace.setDelFlag("1"); //查询未删除数据
		return R.ok(configureGaugeReplaceService.pageCmp(page, configureGaugeReplace));
		//return R.ok(configureGaugeReplaceService.getPageData(page, Wrappers.query(configureGaugeReplace)));
	}


	/**
	 * 分页查询
	 * @return
	 */
	@SysLog("能源计量器具更换记录-分页查询")
	@ApiOperation(value = "查询能源计量器具更换记录",notes = "条件查询能源计量器具更换记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name ="Page",value = "分页对象",required = false,dataType = "json",paramType = "path"),
			@ApiImplicitParam(name ="ConfigureGaugeReplace",value = "能源计量器具",required = false,dataType = "string",paramType = "path")
	})
	@GetMapping("/page")
	public R page(Page page, ConfigureGaugeReplace configureGaugeReplace) {
		configureGaugeReplace.setDelFlag("1"); //查询未删除数据
		return R.ok(configureGaugeReplaceService.pageCmp(page, configureGaugeReplace));
		//return R.ok(configureGaugeReplaceService.getPageData(page, Wrappers.query(configureGaugeReplace)));
	}


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

	/**
	 * 保存/修改能源计量器具更换记录
	 *
	 * @param configureGaugeReplace
	 * @return R
	 */
	@SysLog("能源计量器具更换记录-保存/修改能源计量器具更换记录")
	@ApiOperation(value = "保存/修改能源计量器具更换记录",notes = "保存/修改能源计量器具更换记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name ="ConfigureGaugeReplace",value = "能源计量器具更换记录",required = true,dataType = "json",paramType = "path"),
			@ApiImplicitParam(name = "replacedBarCodeFile", value = "文件流（被替换计量器二维码）", required = false, dataType = "File", paramType = "path"),
			@ApiImplicitParam(name = "installBarCodeFile", value = "文件流（新装计量器二维码）", required = false, dataType = "File", paramType = "path"),
			@ApiImplicitParam(name = "installTimeStr", value = "新装计量器时间字符串", required = false, dataType = "string", paramType = "path")
	})
	@PostMapping
	public R save(ConfigureGaugeReplace configureGaugeReplace, MultipartFile replacedBarCodeFile, MultipartFile installBarCodeFile, String installTimeStr) throws Exception {
		log.error("保存/修改能源计量器具更换记录：{}，##文件1：{}，##文件2：{}",configureGaugeReplace, replacedBarCodeFile , installBarCodeFile );

		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			configureGaugeReplace.setUserid(u.getId());
		}
		if (u.getDeptId() != null) {
			configureGaugeReplace.setDeptId(u.getDeptId());
		}
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        if (installTimeStr != null && !"".equals(installTimeStr)) {
            //configureGaugeReplace.setInstallTime(df.parse(installTimeStr));
            configureGaugeReplace.setInstallTime(new Date(installTimeStr)); //Thu Sep 10 2020 00:00:00 GMT+0800 (中国标准时间)
        }
		configureGaugeReplace.setEnterpriseCode(u.getEnterpriseCode());
		configureGaugeReplace.setDelFlag("1");
		configureGaugeReplace.setUpdateTime(new Date());

		if (replacedBarCodeFile != null) {
			if(replacedBarCodeFile.getSize()>0){
				//被替换计量器二位码
				configureGaugeReplace.setReplacedBarCode(getFileUrl(replacedBarCodeFile));
			}
		}
		if (installBarCodeFile != null) {
			if(replacedBarCodeFile.getSize()>0) {
				//新装计量器二维码
				configureGaugeReplace.setInstallBarCode(getFileUrl(installBarCodeFile));
			}
		}
		return R.ok(configureGaugeReplaceService.saveOrUpdate(configureGaugeReplace));
	}

	@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 configureGaugeReplace
	 * @return R
	 */
	@SysLog("能源计量器具更换记录-修改")
	@ApiOperation(value = "修改能源计量器具更换记录",notes = "修改能源计量器具更换记录")
	@ApiImplicitParam(name ="ConfigureGaugeReplace",value = "能源计量器具更换记录",required = true,dataType = "json",paramType = "path")
	@PutMapping
	public R updateById(@RequestBody ConfigureGaugeReplace configureGaugeReplace) {
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			configureGaugeReplace.setUserid(u.getId());
		}
		configureGaugeReplace.setUpdateTime(new Date());
		return R.ok(configureGaugeReplaceService.updateById(configureGaugeReplace));
	}

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

}
