package org.jeecg.modules.device.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.device.entity.BuRiskItem;
import org.jeecg.modules.device.entity.BuRiskItemValue;
import org.jeecg.modules.device.model.DeviceSensorStatus;
import org.jeecg.modules.device.service.IBuRiskItemService;
import org.jeecg.modules.device.service.IBuRiskItemValueService;
import org.jeecg.modules.sensor.entity.BuSensor;
import org.jeecg.modules.sensor.entity.BuSensorType;
import org.jeecg.modules.sensor.service.IBuSensorTypeService;
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: bu_risk_item
 * @Author: jeecg-boot
 * @Date:   2024-08-02
 * @Version: V1.0
 */
@Api(tags="风险项")
@RestController
@RequestMapping("/device/buRiskItem")
@Slf4j
public class BuRiskItemController extends JeecgController<BuRiskItem, IBuRiskItemService> {
	@Autowired
	private IBuRiskItemService buRiskItemService;
	 @Autowired
	 private IBuRiskItemValueService buRiskItemValueService;
	 @Autowired
	 private IBuSensorTypeService buSensorTypeService;
	/**
	 * 分页列表查询
	 *
	 * @param buRiskItem
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "bu_risk_item-分页列表查询")
	@ApiOperation(value="风险项-分页列表查询", notes="bu_risk_item-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<BuRiskItem>> queryPageList(BuRiskItem buRiskItem,
												   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												   HttpServletRequest req) {
		QueryWrapper<BuRiskItem> queryWrapper = QueryGenerator.initQueryWrapper(buRiskItem, req.getParameterMap());
		queryWrapper.orderByDesc("id");
		Page<BuRiskItem> page = new Page<BuRiskItem>(pageNo, pageSize);
		IPage<BuRiskItem> pageList = buRiskItemService.page(page, queryWrapper);
		for (BuRiskItem record : pageList.getRecords()) {
			if(record.getSensorType()!=null){
				BuSensorType buSensorType = buSensorTypeService.getById(record.getSensorType());
				if(buSensorType!=null){
					record.setSensorTypeName(buSensorType.getName());
				}
			}
		}
		return Result.OK(pageList);
	}
	 @AutoLog(value = "风险项大屏-首页查询")
	 @ApiOperation(value="风险项大屏-首页查询", notes="风险项大屏-首页查询")
	 @GetMapping(value = "/queryRiskItem")
	 public Result<?> queryRiskItem() {
		 List<BuRiskItem> buRiskItemList = buRiskItemService.list();
		 List<List<String>> lists = new ArrayList<>();
		 int m = 1;
		 for(int i=0;i<buRiskItemList.size();i++){
			 List<String> list = new ArrayList<>();
			 list.add(String.valueOf(m));
			 list.add(buRiskItemList.get(i).getRiskName());
			 lists.add(list);
			 m++;
		 }
//		 for (BuRiskItem buRiskItem : buRiskItemList) {
//			 List<String> list = new ArrayList<>();
//			 list.add(buRiskItem.getRiskName());
//			 lists.add(list);
//		 }

		 return Result.OK(lists);
	 }
	 @AutoLog(value = "风险项-分页列表查询")
	 @ApiOperation(value="风险项-分页列表查询", notes="风险项-分页列表查询")
	 @GetMapping(value = "/selectList")
	 public Result<?> selectList(BuRiskItem buRiskItem,
								 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								 HttpServletRequest req) {
//		 QueryWrapper<BuDevice> queryWrapper = QueryGenerator.initQueryWrapper(buDevice, req.getParameterMap());
		 LambdaQueryWrapper<BuRiskItem> queryWrapper = new LambdaQueryWrapper<>();
		 queryWrapper.like(StringUtils.isNotBlank(buRiskItem.getRiskName()),BuRiskItem::getRiskName,buRiskItem.getRiskName());
		 Page<BuRiskItem> page = new Page<BuRiskItem>(pageNo, pageSize);
		 IPage<BuRiskItem> pageList = buRiskItemService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	/**
	 *   添加
	 *
	 * @param buRiskItem
	 * @return
	 */
	@AutoLog(value = "bu_risk_item-添加")
	@ApiOperation(value="bu_risk_item-添加", notes="bu_risk_item-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:bu_risk_item:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody BuRiskItem buRiskItem) {
		buRiskItemService.save(buRiskItem);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param buRiskItem
	 * @return
	 */
	@AutoLog(value = "bu_risk_item-编辑")
	@ApiOperation(value="bu_risk_item-编辑", notes="bu_risk_item-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:bu_risk_item:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody BuRiskItem buRiskItem) {
		buRiskItemService.updateById(buRiskItem);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "bu_risk_item-通过id删除")
	@ApiOperation(value="bu_risk_item-通过id删除", notes="bu_risk_item-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.demo:bu_risk_item:delete")

	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		LambdaQueryWrapper<BuRiskItemValue> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(BuRiskItemValue::getRiskId,id);
		buRiskItemValueService.remove(lambdaQueryWrapper);
		buRiskItemService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "bu_risk_item-批量删除")
	@ApiOperation(value="bu_risk_item-批量删除", notes="bu_risk_item-批量删除")
	//@RequiresPermissions("org.jeecg.modules.demo:bu_risk_item:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		LambdaQueryWrapper<BuRiskItemValue> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.in(BuRiskItemValue::getRiskId,Arrays.asList(ids.split(",")));
		buRiskItemValueService.remove(lambdaQueryWrapper);
		this.buRiskItemService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "bu_risk_item-通过id查询")
	@ApiOperation(value="bu_risk_item-通过id查询", notes="bu_risk_item-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<BuRiskItem> queryById(@RequestParam(name="id",required=true) String id) {
		BuRiskItem buRiskItem = buRiskItemService.getById(id);
		if(buRiskItem==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(buRiskItem);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param buRiskItem
    */
    //@RequiresPermissions("org.jeecg.modules.demo:bu_risk_item:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BuRiskItem buRiskItem) {
        return super.exportXls(request, buRiskItem, BuRiskItem.class, "bu_risk_item");
    }

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

}
