package com.zhgd.xmgl.modules.basicdata.controller.project;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.jeecg.common.util.PageUtil;
import com.zhgd.xmgl.modules.basicdata.entity.EnvironmentDev;
import com.zhgd.xmgl.modules.basicdata.service.IEngineeringService;
import com.zhgd.xmgl.modules.basicdata.service.IEnvironmentDevService;
import com.zhgd.xmgl.security.SecurityUser;
import com.zhgd.xmgl.security.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;


/**
 * @Title: Controller
 * @Description: 扬尘设备管理
 * @author： pds
 * @date： 2023-03-01
 * @version： V1.0
 */
@RestController
@RequestMapping("/project/environmentDev")
@Slf4j
@Api(tags = "扬尘管理")
public class EnvironmentDevController {
	@Autowired
	private IEnvironmentDevService environmentDevService;

	@Autowired
	private IEngineeringService engineeringService;

	/**
	 * 分页列表查询
	 *
	 * @return
	 */
	@OperLog(operModul = "扬尘管理", operType = "分页查询", operDesc = "分页列表查询扬尘设备管理信息")
	@ApiOperation(value = " 分页列表查询扬尘设备管理信息", notes = "分页列表查询扬尘设备管理信息", httpMethod = "POST")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "name", value = "扬尘名称", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "pageNo", value = "页数", paramType = "query", required = true, defaultValue = "1", dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", required = true, defaultValue = "10", dataType = "Integer")
	})
	@PostMapping(value = "/page")
	public Result<IPage<EnvironmentDev>> queryPageList(@RequestBody Map<String, Object> map) {
		String name = MapUtils.getString(map, "name");
		LambdaQueryWrapper<EnvironmentDev> wrapper = Wrappers.<EnvironmentDev>lambdaQuery();
		SecurityUser user = SecurityUtil.getUser();
		if (StringUtils.isNotBlank(user.getEngineeringSn())) {
			wrapper.eq(EnvironmentDev::getEngineeringSn, user.getSn());
		} else {
			wrapper.eq(EnvironmentDev::getProjectSn, user.getSn()).eq(EnvironmentDev::getEngineeringSn, "");
		}
		if (StringUtils.isNotBlank(name)) {
			wrapper.like(EnvironmentDev::getName, name);
		}
		Page<EnvironmentDev> page = PageUtil.getPage(map);
		IPage<EnvironmentDev> pageList = environmentDevService.page(page, wrapper);
		return Result.success(pageList);
	}

	/**
	 * 添加
	 *
	 * @param environmentDev
	 * @return
	 */
	@OperLog(operModul = "扬尘管理", operType = "新增", operDesc = "添加扬尘设备管理信息")
	@ApiOperation(value = " 添加扬尘设备管理信息", notes = "添加扬尘设备管理信息", httpMethod = "POST")
	@PostMapping(value = "/add")
	public Result<EnvironmentDev> add(@RequestBody EnvironmentDev environmentDev) {
		Result<EnvironmentDev> result = new Result<EnvironmentDev>();
		try {
			SecurityUser user = SecurityUtil.getUser();
			environmentDev.setCreateTime(new Date());
			environmentDev.setProjectSn(user.getSn());
			environmentDev.setEngineeringSn(user.getEngineeringSn());
			environmentDevService.save(environmentDev);
			result.success("添加成功！");
		} catch (Exception e) {
			e.printStackTrace();
			log.info(e.getMessage());
			result.error500("操作失败");
		}
		return result;
	}

	/**
	 * 编辑
	 *
	 * @param environmentDev
	 * @return
	 */
	@OperLog(operModul = "扬尘管理", operType = "修改", operDesc = "编辑扬尘设备管理信息")
	@ApiOperation(value = "编辑扬尘设备管理信息", notes = "编辑扬尘设备管理信息", httpMethod = "POST")
	@PostMapping(value = "/edit")
	public Result<EnvironmentDev> edit(@RequestBody EnvironmentDev environmentDev) {
		Result<EnvironmentDev> result = new Result<EnvironmentDev>();
		EnvironmentDev environmentDevEntity = environmentDevService.getById(environmentDev.getId());
		if (environmentDevEntity == null) {
			result.error500("未找到对应实体");
		} else {
			SecurityUser user = SecurityUtil.getUser();
			environmentDev.setProjectSn(user.getSn());
			environmentDev.setEngineeringSn(user.getEngineeringSn());
			boolean ok = environmentDevService.updateById(environmentDev);
			if (ok) {
				result.success("修改成功!");
			}
		}
		return result;
	}

	/**
	 * 通过id删除
	 * @return
	 */
	@OperLog(operModul = "扬尘管理", operType = "删除", operDesc = "删除扬尘设备管理信息")
	@ApiOperation(value = "删除扬尘设备管理信息", notes = "删除扬尘设备管理信息", httpMethod = "POST")
	@ApiImplicitParam(name = "id", value = "扬尘设备管理ID", paramType = "query", required = true, dataType = "Integer")
	@PostMapping(value = "/delete")
	public Result<EnvironmentDev> delete(@RequestBody EnvironmentDev environmentDev) {
		Result<EnvironmentDev> result = new Result<EnvironmentDev>();
		EnvironmentDev iEnvironmentDev = environmentDevService.getById(environmentDev.getId());
		if (iEnvironmentDev == null) {
			result.error500("未找到对应实体");
		} else {
			boolean ok = environmentDevService.removeById(environmentDev.getId());
			if (ok) {
				result.success("删除成功!");
			}
		}
		return result;
	}
}
