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.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.BuDevice;
import org.jeecg.modules.device.entity.BuRiskItem;
import org.jeecg.modules.device.entity.BuRiskItemValue;
import org.jeecg.modules.device.service.IBuDeviceService;
import org.jeecg.modules.device.service.IBuRiskItemService;
import org.jeecg.modules.device.service.IBuRiskItemValueService;
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_value
 * @Author: jeecg-boot
 * @Date:   2024-08-02
 * @Version: V1.0
 */
@Api(tags="bu_risk_item_value")
@RestController
@RequestMapping("/device/buRiskItemValue")
@Slf4j
public class BuRiskItemValueController extends JeecgController<BuRiskItemValue, IBuRiskItemValueService> {
	@Autowired
	private IBuRiskItemValueService buRiskItemValueService;
	@Autowired
	private IBuRiskItemService buRiskItemService;
	@Autowired
	private IBuDeviceService buDeviceService;
	/**
	 * 分页列表查询
	 *
	 * @param buRiskItemValue
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "bu_risk_item_value-分页列表查询")
	@ApiOperation(value="bu_risk_item_value-分页列表查询", notes="bu_risk_item_value-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<BuRiskItemValue>> queryPageList(BuRiskItemValue buRiskItemValue,
														@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														HttpServletRequest req) {
//		QueryWrapper<BuRiskItemValue> queryWrapper = QueryGenerator.initQueryWrapper(buRiskItemValue, req.getParameterMap());
		LambdaQueryWrapper<BuRiskItemValue> queryWrapper = new LambdaQueryWrapper<>();
		if(buRiskItemValue.getDeviceId()!=null){
			queryWrapper.eq(BuRiskItemValue::getDeviceId,buRiskItemValue.getDeviceId());
		}
		if(buRiskItemValue.getRiskId()!=null){
			queryWrapper.eq(BuRiskItemValue::getRiskId,buRiskItemValue.getRiskId());
		}
		Page<BuRiskItemValue> page = new Page<BuRiskItemValue>(pageNo, pageSize);
		IPage<BuRiskItemValue> pageList = buRiskItemValueService.page(page, queryWrapper);
		for (BuRiskItemValue record : pageList.getRecords()) {
			if(record.getRiskId()!=null){
				BuRiskItem buRiskItem = buRiskItemService.getById(record.getRiskId());
				if(buRiskItem!=null){
					record.setRiskName(buRiskItem.getRiskName());
				}
			}
			if(record.getDeviceId()!=null){
				BuDevice buDevice = buDeviceService.getById(record.getDeviceId());
				if(buDevice!=null){
					record.setDeviceName(buDevice.getDeviceName()+buDevice.getDeviceCode());
				}
			}
		}
		return Result.OK(pageList);
	}
	 @AutoLog(value = "风险项大屏-首页查询")
	 @ApiOperation(value="风险项大屏-首页查询", notes="风险项大屏-首页查询")
	 @GetMapping(value = "/queryRiskItemValue")
	 public Result<?> queryRiskItem(String deviceId) {
		LambdaQueryWrapper<BuRiskItemValue> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		 lambdaQueryWrapper.eq(BuRiskItemValue::getDeviceId,deviceId);
		 lambdaQueryWrapper.eq(BuRiskItemValue::getEnabled,"0");
		 List<BuRiskItemValue> buRiskItemValueList = buRiskItemValueService.list(lambdaQueryWrapper);
		 List<List<Object>> lists = new ArrayList<>();
		 int m = 1;
		 for(int i=0;i<buRiskItemValueList.size();i++){
			 List<Object> list = new ArrayList<>();
			 list.add(String.valueOf(m));
			 list.add(buRiskItemService.getById(buRiskItemValueList.get(i).getRiskId()).getRiskName());
			 list.add(buRiskItemValueList.get(i).getStatus());
			 lists.add(list);
			 m++;
		 }
		 return Result.OK(lists);
	 }
	 public List<List<Object>> queryRiskItems(String deviceId) {
		 LambdaQueryWrapper<BuRiskItemValue> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		 lambdaQueryWrapper.eq(BuRiskItemValue::getDeviceId,deviceId);
		 lambdaQueryWrapper.eq(BuRiskItemValue::getEnabled,"0");
		 List<BuRiskItemValue> buRiskItemValueList = buRiskItemValueService.list(lambdaQueryWrapper);
		 List<List<Object>> lists = new ArrayList<>();
		 int m = 1;
		 for(int i=0;i<buRiskItemValueList.size();i++){
			 List<Object> list = new ArrayList<>();
			 list.add(String.valueOf(m));
			 list.add(buRiskItemService.getById(buRiskItemValueList.get(i).getRiskId()).getRiskName());
			 list.add(buRiskItemValueList.get(i).getStatus());
			 lists.add(list);
			 m++;
		 }
		 return lists;
	 }

	/**
	 *   添加
	 *
	 * @param buRiskItemValue
	 * @return
	 */
	@AutoLog(value = "bu_risk_item_value-添加")
	@ApiOperation(value="bu_risk_item_value-添加", notes="bu_risk_item_value-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:bu_risk_item_value:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody BuRiskItemValue buRiskItemValue) {
		String msg = "";
		if(buRiskItemValue.getDeviceId()!=null){
			LambdaQueryWrapper<BuRiskItemValue> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.eq(BuRiskItemValue::getDeviceId,buRiskItemValue.getDeviceId());
			queryWrapper.eq(BuRiskItemValue::getRiskId,buRiskItemValue.getRiskId());
			List<BuRiskItemValue> list = buRiskItemValueService.list(queryWrapper);
			if(list.size()==0){
				buRiskItemValue.setStatus(1);
				buRiskItemValueService.save(buRiskItemValue);
				msg = "添加成功！";
			}else {
				msg = "已存在该设备的检测项！";
			}
		}
		return Result.OK(msg);
	}

	/**
	 *  编辑
	 *
	 * @param buRiskItemValue
	 * @return
	 */
	@AutoLog(value = "bu_risk_item_value-编辑")
	@ApiOperation(value="bu_risk_item_value-编辑", notes="bu_risk_item_value-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:bu_risk_item_value:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody BuRiskItemValue buRiskItemValue) {
		buRiskItemValueService.updateById(buRiskItemValue);
		return Result.OK("编辑成功!");
	}

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

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

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "bu_risk_item_value-通过id查询")
	@ApiOperation(value="bu_risk_item_value-通过id查询", notes="bu_risk_item_value-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<BuRiskItemValue> queryById(@RequestParam(name="id",required=true) String id) {
		BuRiskItemValue buRiskItemValue = buRiskItemValueService.getById(id);
		if(buRiskItemValue==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(buRiskItemValue);
	}

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

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

}
