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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.system.query.QueryGenerator;
import com.zhgd.jeecg.common.util.PageUtil;
import com.zhgd.xmgl.modules.basicdata.dto.GovernmentDto;
import com.zhgd.xmgl.modules.basicdata.entity.Government;
import com.zhgd.xmgl.modules.basicdata.entity.SystemUser;
import com.zhgd.xmgl.modules.basicdata.service.IGovernmentService;
import com.zhgd.xmgl.modules.basicdata.service.ISystemUserService;
import com.zhgd.xmgl.modules.basicdata.vo.GovernmentVo;
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.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 javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


 /**
 * @Title: Controller
 * @Description: 政务信息
 * @author： pds
 * @date：   2023-02-15
 * @version： V1.0
 */
@RestController
@RequestMapping("/xmgl/government")
@Slf4j
@Api(tags = "政务信息")
public class GovernmentController {
	@Autowired
	private IGovernmentService governmentService;

	@Autowired
	private ISystemUserService systemUserService;
	
	/**
	  * 分页列表查询
	 * @return
	 */
	@OperLog(operModul = "政务管理", operType = "分页查询", operDesc = "分页列表查询政务信息")
	@ApiOperation(value = " 分页列表查询政务信息", notes = "分页列表查询政务信息", httpMethod="POST")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "governmentName", 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<Government>> queryPageList(@RequestBody Map<String, Object> map) {
		Result<IPage<Government>> result = new Result<IPage<Government>>();
		QueryWrapper<Government> queryWrapper = QueryGenerator.initPageQueryWrapper(Government.class, map);
		Page<Government> page = PageUtil.getPage(map);
		IPage<Government> pageList = governmentService.selectPage(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

	 /**
	  * 列表查询
	  * @param government
	  * @param req
	  * @return
	  */
	 @OperLog(operModul = "政务管理", operType = "列表查询", operDesc = "列表查询政务信息")
	 @ApiOperation(value = " 列表查询政务信息", notes = "列表查询政务信息", httpMethod="POST")
	 @PostMapping(value = "/list")
	 public Result<List<Government>> queryList(@RequestBody Government government, HttpServletRequest req) {
		 Result<List<Government>> result = new Result<List<Government>>();
		 QueryWrapper<Government> queryWrapper = QueryGenerator.initQueryWrapper(government, req.getParameterMap());
		 List<Government> list = governmentService.list(queryWrapper);
		 result.setSuccess(true);
		 result.setResult(list);
		 return result;
	 }
	
	/**
	  *   添加
	 * @param governmentVo
	 * @return
	 */
	@OperLog(operModul = "政务管理", operType = "新增", operDesc = "添加政务信息")
	@ApiOperation(value = " 添加政务信息", notes = "添加政务信息" , httpMethod="POST")
	@PostMapping(value = "/add")
	public Result<Government> add(@RequestBody GovernmentVo governmentVo) {
		Result<Government> result = new Result<Government>();
		try {
			// 检验政务账号是否已存在
			SystemUser systemUser = systemUserService.getOne(Wrappers.<SystemUser>lambdaQuery().eq(SystemUser::getAccount, governmentVo.getAccount()));
			if (systemUser != null) {
				result.error500("该用户账号已被使用");
				return result;
			}
			governmentService.saveInfo(governmentVo);
			result.success("添加成功！");
		} catch (Exception e) {
			e.printStackTrace();
			log.info(e.getMessage());
			result.error500("操作失败");
		}
		return result;
	}
	
	/**
	  *  编辑
	 * @param governmentVo
	 * @return
	 */
	@OperLog(operModul = "政务管理", operType = "修改", operDesc = "编辑政务信息")
	@ApiOperation(value = "编辑政务信息", notes = "编辑政务信息" , httpMethod="POST")
	@PostMapping(value = "/edit")
	public Result<Government> edit(@RequestBody GovernmentVo governmentVo) {
		Result<Government> result = new Result<Government>();
		Government governmentEntity = governmentService.getById(governmentVo.getGovernmentId());
		if(governmentEntity==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = governmentService.updateInfo(governmentVo) == 1 ? true : false;
			if(ok) {
				result.success("修改成功!");
			}
		}
		return result;
	}
	
	/**
	  *   通过id删除
	 * @return
	 */
	@OperLog(operModul = "政务管理", operType = "删除", operDesc = "删除政务信息")
	@ApiOperation(value = "删除政务信息", notes = "删除政务信息" , httpMethod="POST")
	@ApiImplicitParam(name = "governmentId", value = "政务信息ID", paramType = "query", required = true, dataType = "Integer")
	@PostMapping(value = "/delete")
	public Result<Government> delete(@RequestBody Government government) {
		Result<Government> result = new Result<Government>();
		Government governmentEntity = governmentService.getById(government.getGovernmentId());
		if(governmentEntity==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = governmentService.removeById(government.getGovernmentId());
			if(ok) {
				result.success("删除成功!");
			}
		}
		
		return result;
	}
	
	/**
	  * 通过id查询
	 * @return
	 */
	@OperLog(operModul = "政务管理", operType = "查询", operDesc = "通过id查询政务信息")
	@ApiOperation(value = "通过id查询政务信息", notes = "通过id查询政务信息" , httpMethod="POST")
    @ApiImplicitParam(name = "governmentId", value = "政务信息ID", paramType = "query", required = true, dataType = "Integer")
	@PostMapping(value = "/queryById")
	public Result<Government> queryById(@RequestBody Government governmentEntity) {
		Result<Government> result = new Result<Government>();
		Government government = governmentService.getById(governmentEntity.getGovernmentId());
		if(government==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(government);
			result.setSuccess(true);
		}
		return result;
	}

	 /**
	  * 通过id查询政务以及账号信息
	  * @return
	  */
	 @OperLog(operModul = "政务管理", operType = "查询", operDesc = "通过id查询政务以及账号信息")
	 @ApiOperation(value = "通过id查询政务以及账号信息", notes = "通过id查询政务以及账号信息" , httpMethod="POST")
	 @ApiImplicitParam(name = "governmentId", value = "政务信息ID", paramType = "query", required = true, dataType = "Integer")
	 @PostMapping(value = "/queryDetailById")
	 public Result<GovernmentDto> queryDetailById(@RequestBody Government governmentEntity) {
		 Result<GovernmentDto> result = new Result<GovernmentDto>();
		 Government government = governmentService.getById(governmentEntity.getGovernmentId());
		 if(government==null) {
			 result.error500("未找到对应实体");
		 }else {
			 result.setResult(governmentService.getDetailById(government));
			 result.setSuccess(true);
		 }
		 return result;
	 }
}
