package org.springblade.workorder.deviceorder.feign;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.community.entity.Residential;
import org.springblade.community.feign.IResidentialClient;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.device.entity.IotAlarm;
import org.springblade.device.entity.IotDevice;
import org.springblade.device.feign.IIotAlarmClient;
import org.springblade.device.feign.IIotDeviceClient;
import org.springblade.device.vo.IotAlarmVO;
import org.springblade.device.vo.IotDeviceVO;
import org.springblade.workorder.deviceorder.dto.DeviceOrderDTO;
import org.springblade.workorder.deviceorder.entity.DeviceOrder;
import org.springblade.workorder.deviceorder.service.IDeviceOrderService;
import org.springblade.workorder.deviceorder.vo.DeviceOrderStatisticsVO;
import org.springblade.workorder.deviceorder.vo.DeviceOrderVO;
import org.springblade.workorder.deviceorder.wrapper.DeviceOrderWrapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Objects;

/**
 * 设备工单 client
 *
 * @author lianghanqiang
 * @since 2021-04-21
 */

@AllArgsConstructor
@RestController
@RequestMapping("/feign/deviceorder")

public class DeviceOrderClient implements IDeviceOrderClient {

	private final IDeviceOrderService deviceOrderService;
	private final IIotDeviceClient iotDeviceClient;
	private final IResidentialClient residentialClient;
	private final IIotAlarmClient iotAlarmClient;

        /**
     * 详情
     */
    @Override
    @GetMapping("/detail")
    public DeviceOrderVO detail(DeviceOrder deviceOrder) {
        DeviceOrder detail = deviceOrderService.getOne(Condition.getQueryWrapper(deviceOrder));
        return DeviceOrderWrapper.build().entityVO(detail);
        }

    /**
     * 查询 设备工单
     */
    @Override
    @GetMapping("/list")
    public List<DeviceOrderVO> list(DeviceOrder deviceOrder) {
        return DeviceOrderWrapper.build().listVO(deviceOrderService.list(Condition.getQueryWrapper(deviceOrder)));
        }



    /**
     * 新增 设备工单
     */
    @Override
    @PostMapping("/save")
    public boolean save(@Valid @RequestBody DeviceOrder deviceOrder) {
        return deviceOrderService.save(deviceOrder);
        }

    /**
     * 修改 设备工单
     */
    @Override
    @PostMapping("/update")
    public boolean update(@Valid @RequestBody DeviceOrder deviceOrder) {
        return deviceOrderService.updateById(deviceOrder);
        }

	/**
	 * 新增或修改 设备工单
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入deviceOrder")
	@Override
	public R submit(@Valid @RequestBody DeviceOrderVO deviceOrder) {
		Long alarmId = deviceOrder.getId();
		deviceOrder.setId(null);
		deviceOrder.setAlarmTime(deviceOrder.getRecordTime());
		deviceOrder.setAlarmReason(deviceOrder.getName());
		deviceOrder.setJobStatus(1);//处理中

		IotDevice iotDevice = new IotDevice();
		iotDevice.setDeviceId(deviceOrder.getDeviceId());
		IotDeviceVO deviceVO = iotDeviceClient.detail(iotDevice);
		if (deviceVO != null) {
			deviceOrder.setAlarmPosition(deviceVO.getDeviceInstallAddress());
			Residential residential = residentialClient.getById(deviceVO.getResidentialId());
			if (residential == null) {
				System.out.println("找不到该小区！！！！");
				R.status(false);
			}
			deviceOrder.setHandlePerson(residential.getPersonName());
			deviceOrder.setHandlePhone(residential.getPersonPhone());
			deviceOrder.setTenantId(residential.getTenantId());
		}
		boolean result = deviceOrderService.saveOrUpdate(deviceOrder);
		//设置告警记录的工单id
		IotAlarm iotAlarm = new IotAlarm();
		iotAlarm.setId(alarmId);
		IotAlarmVO iotAlarmVO = iotAlarmClient.detail(iotAlarm);
		if(iotAlarmVO!=null){
			iotAlarmVO.setOrderId(deviceOrder.getId().toString());
			iotAlarmClient.update(iotAlarmVO);
		}

		return R.status(result);
	}


    /**
     * 删除 设备工单
     */
    @Override
    @PostMapping("/remove")
    public boolean remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return deviceOrderService.deleteLogic(Func.toLongList(ids));
        }

	@Override
	@PostMapping("/getListByDeviceTypeList")
	public List<DeviceOrder> getListByDeviceTypeList(@RequestBody DeviceOrderDTO deviceOrderDTO) {
		DeviceOrder deviceOrder = Objects.requireNonNull(BeanUtil.copy(deviceOrderDTO, DeviceOrder.class));
		return this.deviceOrderService.list(Condition.getQueryWrapper(deviceOrder)
				.lambda().in(deviceOrderDTO.getDeviceTypeList().size()>0,DeviceOrder::getDeviceType,deviceOrderDTO.getDeviceTypeList()));
	}

	/**
	 * 设备工单分类统计
	 */
	@Override
	@GetMapping("/getDeviceJobStaticVo")
	public R<DeviceOrderStatisticsVO> getDeviceJobStaticVo() {

		DeviceOrderStatisticsVO deviceJobStaticVO = new DeviceOrderStatisticsVO();

		/*//烟感总数
		Integer smokeCount = deviceOrderService.getBaseMapper().selectCount(new QueryWrapper<>(new DeviceOrder()).lambda().eq(DeviceOrder::getDeviceType, 100004).or().eq(DeviceOrder::getDeviceType,10000002));
		//燃气监控总数
		Integer gasCount = deviceOrderService.getBaseMapper().selectCount(new QueryWrapper<>(new DeviceOrder()).lambda().eq(DeviceOrder::getDeviceType, 100020).or().eq(DeviceOrder::getDeviceType,10000017));
		//井盖监控总数
		Integer coverCount = deviceOrderService.getBaseMapper().selectCount(new QueryWrapper<>(new DeviceOrder()).lambda().eq(DeviceOrder::getDeviceType, 3));
		//一键告警总数
		Integer alarmCount = deviceOrderService.getBaseMapper().selectCount(new QueryWrapper<>(new DeviceOrder()).lambda().eq(DeviceOrder::getDeviceType, 4));
		//智慧路灯总数
		Integer lampCount = deviceOrderService.getBaseMapper().selectCount(new QueryWrapper<>(new DeviceOrder()).lambda().eq(DeviceOrder::getDeviceType, 5));
		//智慧消防
		Integer xfCount = deviceOrderService.getBaseMapper().selectCount(new QueryWrapper<>(new DeviceOrder()).lambda().eq(DeviceOrder::getDeviceType, 960417));
		//垃圾桶
		Integer trashBinCount = deviceOrderService.getBaseMapper().selectCount(new QueryWrapper<>(new DeviceOrder()).lambda().eq(DeviceOrder::getDeviceType, 7));
		//停车监控
		Integer parkCount = deviceOrderService.getBaseMapper().selectCount(new QueryWrapper<>(new DeviceOrder()).lambda().eq(DeviceOrder::getDeviceType, 8));
		//门磁
		Integer gateCount = deviceOrderService.getBaseMapper().selectCount(new QueryWrapper<>(new DeviceOrder()).lambda().eq(DeviceOrder::getDeviceType, 9));

		deviceJobStaticVO.setAlarmCount(alarmCount);
		deviceJobStaticVO.setCoverCount(coverCount);
		deviceJobStaticVO.setGasCount(gasCount);
		deviceJobStaticVO.setGateCount(gateCount);
		deviceJobStaticVO.setLampCount(lampCount);
		deviceJobStaticVO.setParkCount(parkCount);
		deviceJobStaticVO.setSmokeCount(smokeCount);
		deviceJobStaticVO.setTrashbinCount(trashBinCount);
		deviceJobStaticVO.setXfCount(xfCount);*/

		deviceJobStaticVO.setGasCount(getDeviceCount(100020,10000017));//燃气监控总数
		deviceJobStaticVO.setSmokeCount(getDeviceCount(100004,10000002));//烟感总数
		deviceJobStaticVO.setAlarmCount(getDeviceCount(950414,0));//一键告警总数
		deviceJobStaticVO.setCoverCount(getDeviceCount(10000001,0));//井盖监控总数
		deviceJobStaticVO.setLampCount(getDeviceCount(100011,0));//智慧路灯总数
		deviceJobStaticVO.setParkCount(getDeviceCount(100007,0));//地磁
		deviceJobStaticVO.setTrashbinCount(getDeviceCount(100022,0));//垃圾桶
		deviceJobStaticVO.setXfCount(getDeviceCount(960417,0));//智慧消防


		return R.data(deviceJobStaticVO);
	}

	/**
	 * 根据设备类型查询工单数量
	 * @param i 设备类型
	 * @param j 设备类型
	 * @return 工单数量
	 */
	private Integer getDeviceCount(int i, int j) {
		return deviceOrderService.getBaseMapper().selectCount(new QueryWrapper<>(new DeviceOrder()).lambda()
			.eq(DeviceOrder::getDeviceType, i).or(j != 0, qw -> qw.eq(DeviceOrder::getDeviceType, j)));
	}


}
