package org.jeecg.modules.itdmMima.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.modules.itdmDevice.entity.ItdmDevice;
import org.jeecg.modules.itdmDevice.service.IItdmDeviceService;
import org.jeecg.modules.itdmMima.dto.ItdmMimaDTO;
import org.jeecg.modules.itdmMima.entity.ItdmMima;
import org.jeecg.modules.itdmMima.mapper.ItdmMimaMapper;
import org.jeecg.modules.itdmMima.service.IItdmMimaService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 修改密码表
 * @Author: jeecg-boot
 * @Date:   2023-05-27
 * @Version: V1.0
 */
@Api(tags="修改密码表")
@RestController
@RequestMapping("/itdmMima/itdmMima")
@Slf4j
public class ItdmMimaController extends JeecgController<ItdmMima, IItdmMimaService> {
	@Autowired
	@SuppressWarnings("all")
	private IItdmMimaService itdmMimaService;
	@Autowired
	@SuppressWarnings("all")
	private IItdmDeviceService iItdmDeviceService;

	 /**
	  *  判断是否输入正确密码
	  */
	 @AutoLog(value = "判断是否输入正确密码")
	 @ApiOperation(value="判断是否输入正确密码", notes="修改密码表-判断是否输入正确密码，是的话直接修改设备表该设备id对应的设备")
	 //@RequiresPermissions("org.jeecg.modules:itdm_mima:panduan")
	 @GetMapping(value = "/panduan")
	 public Result<String> panduan(ItdmMimaDTO dto) {
		 String passwordEncodeYuan = itdmMimaService.getById(dto.getId()).getMima();
		 String passwordEncodeNow = PasswordUtil.encrypt("admin", dto.getMima(), "RCGTeGiH");
		 if (!passwordEncodeYuan.equals(passwordEncodeNow)) {
			 return Result.error("密码输入错误!");
		 }else {
			 ItdmDevice itdmDevice = new ItdmDevice();
			 itdmDevice.setId(dto.getDeviceId());
			 itdmDevice.setIsRepair(1);
			 boolean resuit = iItdmDeviceService.updateById(itdmDevice);
			 if(resuit==true) return Result.OK("编辑成功!");
			 else return Result.error("编辑失败!");
		 }
	 }

	 /**
	  *  编辑
	  *
	  * @param dto
	  * @return
	  */
	 @AutoLog(value = "修改密码表-编辑")
	 @ApiOperation(value="修改密码表-编辑", notes="修改密码表-编辑")
	 //@RequiresPermissions("org.jeecg.modules:itdm_mima:edit")
	 @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> edit(@RequestBody ItdmMimaDTO dto) {
		 ItdmMima itdmMimaYuan = itdmMimaService.getById(dto.getId());
		 String passwordEncode = PasswordUtil.encrypt("admin", dto.getYuanmima(), "RCGTeGiH");
		 if (!itdmMimaYuan.getMima().equals(passwordEncode)) {
			 return Result.error("旧密码输入错误!");
		 }else {
			 ItdmMima itdmMima = dto.toItdmMima();
			 itdmMima.setMima(PasswordUtil.encrypt("admin", dto.getMima(), "RCGTeGiH"));
			 itdmMimaService.updateById(itdmMima);
			 return Result.OK("编辑成功!");
		 }
	 }


	/**
	 * 分页列表查询
	 *
	 * @param itdmMima
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "修改密码表-分页列表查询")
	@ApiOperation(value="修改密码表-分页列表查询", notes="修改密码表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ItdmMima>> queryPageList(ItdmMima itdmMima,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ItdmMima> queryWrapper = QueryGenerator.initQueryWrapper(itdmMima, req.getParameterMap());
		Page<ItdmMima> page = new Page<ItdmMima>(pageNo, pageSize);
		IPage<ItdmMima> pageList = itdmMimaService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 //@AutoLog(value = "修改密码表-通过id查询")
	 @ApiOperation(value="修改密码表-通过id查询", notes="修改密码表-通过id查询")
	 @GetMapping(value = "/queryById")
	 public Result<ItdmMima> queryById(@RequestParam(name="id",required=true) String id) {
		 ItdmMima itdmMima = itdmMimaService.getById(id);
		 if(itdmMima==null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(itdmMima);
	 }










	/**
	 *   添加
	 *
	 * @param itdmMima
	 * @return
	 */
	@AutoLog(value = "修改密码表-添加")
//	@ApiOperation(value="修改密码表-添加", notes="修改密码表-添加")
	//@RequiresPermissions("org.jeecg.modules:itdm_mima:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ItdmMima itdmMima) {
		itdmMimaService.save(itdmMima);
		return Result.OK("添加成功！");
	}



	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "修改密码表-通过id删除")
//	@ApiOperation(value="修改密码表-通过id删除", notes="修改密码表-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:itdm_mima:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		itdmMimaService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "修改密码表-批量删除")
//	@ApiOperation(value="修改密码表-批量删除", notes="修改密码表-批量删除")
	//@RequiresPermissions("org.jeecg.modules:itdm_mima:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.itdmMimaService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}



    /**
    * 导出excel
    *
    * @param request
    * @param itdmMima
    */
    //@RequiresPermissions("org.jeecg.modules:itdm_mima:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ItdmMima itdmMima) {
        return super.exportXls(request, itdmMima, ItdmMima.class, "修改密码表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("itdm_mima:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ItdmMima.class);
    }

}
