package com.iformall.controller.device;

import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.KwBox;
import com.iformall.domain.po.KwMeter;
import com.iformall.enums.EnumBoxStatus;
import com.iformall.enums.EnumMeterBindStatus;
import com.iformall.enums.EnumMeterStatus;
import com.iformall.service.kw.KwBoxService;
import com.iformall.service.kw.KwMeterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("kwAdminBox")
@Api(description="开物盒子")
public class KwBoxController extends BaseController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
    private KwBoxService kwBoxService;

	@Autowired
	private KwMeterService kwMeterService;

	@ApiOperation("设备列表")
	@GetMapping("list")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "开物盒子-设备列表")
	public Result list(@ModelAttribute KwBox kwBox, Integer pageNum, Integer pageSize) {
		if (kwBox == null) kwBox = new KwBox();
		kwBox.setTenantId(getTenantId());
		kwBox.setStatus(EnumBoxStatus.VALID.getCode());
		return new ResultData(kwBoxService.listAsPage(kwBox, pageNum,pageSize));
	}

    @ApiOperation("添加设备")
    @PostMapping("add")
    @SystemControllerLog(description = "开物盒子-添加设备")
    public Result add(@RequestBody KwBox kwBox) {

		KwBox record = new KwBox();
		record.setImei(kwBox.getImei());
		record.setStatus(EnumBoxStatus.VALID.getCode());
		List<KwBox> list = kwBoxService.findList(record);
		if (list.size() > 0)
			return new ResultData(ErrorCode.DEVICE_ALREADY_EXIST);
		kwBox.setTenantId(getTenantId());
		kwBox.setStatus(EnumBoxStatus.VALID.getCode());
    	kwBoxService.saveOrUpdate(kwBox);
		return new ResultData(kwBox);
    }

	@ApiOperation("更新设备")
	@PostMapping("update")
    @SystemControllerLog(description = "开物盒子-更新设备")
	public Result update(@RequestBody KwBox kwBox) {
    	if (kwBox == null)
    		return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		if (kwBox.getId() == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		kwBox.setImei(null);
		kwBox.setIccid(null);
		kwBox.setComKey(null);
		kwBoxService.saveOrUpdate(kwBox);
		return new ResultData(kwBox);
	}

	@ApiOperation("设备详情")
	@GetMapping("detail")
    @SystemControllerLog(description = "开物盒子-设备详情")
	public Result detail(@RequestParam Long id) {
		if (id == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		KwBox kwBox = kwBoxService.getById(id);
		if (kwBox == null)
			return new ResultData(ErrorCode.DEVICE_NOT_FOUND);

		KwMeter kwMeter = new KwMeter();
		kwMeter.setBoxId(kwBox.getId());
		kwMeter.setBindStatus(EnumMeterBindStatus.BIND.getCode());
		kwMeter.setStatus(EnumMeterStatus.VALID.getCode());
		kwBox.setMeters(kwMeterService.findVoList(kwMeter));
		return new ResultData(kwBox);
	}

	@ApiOperation("删除设备")
	@PostMapping("delete")
    @SystemControllerLog(description = "开物盒子-删除设备")
	public Result delete(@RequestParam Long id) {
		if (id == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		KwBox kwBox = new KwBox();
		kwBox.setId(id);
		kwBox.setStatus(EnumBoxStatus.INVALID.getCode());
		kwBoxService.saveOrUpdate(kwBox);
		kwMeterService.unbindBatch(id);
		return new ResultData();
	}

	@ApiOperation("绑定设备")
	@PostMapping("bind")
	@SystemControllerLog(description = "电表设备-添加设备")
	public Result bind(@RequestBody KwMeter kwMeter) {

		if (kwMeter == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		if (kwMeter.getBoxId() == null ||
				(kwMeter.getId() == null && kwMeter.getIds() == null))
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);

		KwBox kwBox = kwBoxService.getById(kwMeter.getBoxId());
		if (kwBox == null || kwBox.getStatus().equals(EnumBoxStatus.INVALID.getCode()))
			return new ResultData(ErrorCode.DEVICE_NOT_FOUND);


		KwMeter record = new KwMeter();
		record.setBoxId(kwMeter.getBoxId());
		record.setBindStatus(EnumMeterBindStatus.BIND.getCode());
		record.setStatus(EnumMeterStatus.VALID.getCode());
		List<KwMeter> KwMeters = kwMeterService.findList(record);

		if (kwMeter.getId() != null) {
			if (KwMeters.size()  >= kwBox.getMaxTagNum())
				return new ResultData(ErrorCode.DEVICE_REACHED_MAX_NUMBER);
			record = kwMeterService.getById(kwMeter.getId());
			if (record == null)
				return new ResultData(ErrorCode.DEVICE_NOT_FOUND);
			if (record.getBoxId() != 0 && record.getBindStatus().equals(EnumMeterBindStatus.BIND.getCode()))
				return new ResultData(ErrorCode.DEVICE_ALREADY_EXIST);
			kwMeterService.bind(kwMeter.getBoxId(),kwMeter.getId());
		} else {
			if (kwMeter.getIds() != null) {
				if (KwMeters.size() + kwMeter.getIds().size() > kwBox.getMaxTagNum())
					return new ResultData(ErrorCode.DEVICE_REACHED_MAX_NUMBER);
			}
			kwMeterService.bindBatch(kwMeter.getBoxId(),kwMeter.getIds());
		}

		return new ResultData();
	}

	@ApiOperation("绑定设备")
	@PostMapping("unbind")
	@SystemControllerLog(description = "电表设备-添加设备")
	public Result unbind(@RequestBody KwMeter kwMeter) {

		if (kwMeter == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		if (kwMeter.getBoxId() == null && kwMeter.getId() == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);

		if (kwMeter.getId() != null) {
			KwMeter record = kwMeterService.getById(kwMeter.getId());
			if (record == null)
				return new ResultData(ErrorCode.DEVICE_NOT_FOUND);
			kwMeterService.unbind(kwMeter.getId());
		} else {
			if (kwMeter.getBoxId() != null) {
				KwBox kwBox = kwBoxService.getById(kwMeter.getBoxId());
				if (kwBox == null)
					return new ResultData(ErrorCode.DEVICE_NOT_FOUND);
				kwMeterService.unbindBatch(kwMeter.getBoxId());
			}
		}

		return new ResultData();
	}

}
