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.dto.KwMerchantMeterDto;
import com.iformall.domain.po.KwMerchantMeter;
import com.iformall.domain.vo.KwMerchantMeterVo;
import com.iformall.enums.EnumMerchantMeterStatus;
import com.iformall.service.kw.KwMerchantMeterService;
import com.iformall.service.kw.KwMeterDataService;
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.*;


@RestController
@RequestMapping("kwMerchantMeter")
@Api(description="电表设备")
public class KwMerchantMeterController extends BaseController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
    private KwMerchantMeterService kwMerchantMeterService;

	@Autowired
	private KwMeterDataService kwMeterDataService;

	@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 KwMerchantMeterDto kwMerchantMeterDto, Integer pageNum, Integer pageSize) {
		if (kwMerchantMeterDto == null) kwMerchantMeterDto = new KwMerchantMeterDto();
		kwMerchantMeterDto.setTenantId(getTenantId());
		return new ResultData(kwMerchantMeterService.listVoAsPage(kwMerchantMeterDto, pageNum, pageSize));
	}

	@ApiOperation("商户详情")
	@GetMapping("detail")
	@SystemControllerLog(description = "商户详情")
	public Result detail(@RequestParam Long id) {
		if (id == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		KwMerchantMeterVo kwMerchantMeterVo = kwMerchantMeterService.getById(id);
		if (kwMerchantMeterVo == null)
			return new ResultData(ErrorCode.MERCHANT_INFO_NOT_FOUND);
		return new ResultData(kwMerchantMeterVo);
	}

    @ApiOperation("绑定设备")
    @PostMapping("bind")
    @SystemControllerLog(description = "电表设备-绑定设备")
    public Result bind(@RequestBody KwMerchantMeter kwMerchantMeter) {

		if (kwMerchantMeter == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		if (kwMerchantMeter.getMerchantId() == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		if (kwMerchantMeter.getMeterId() == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);

		KwMerchantMeter record = new KwMerchantMeter();
		record.setMeterId(kwMerchantMeter.getMeterId());
		record.setStatus(EnumMerchantMeterStatus.VALID.getCode());
		if (kwMerchantMeterService.selectCount(record) > 0)
			return new ResultData(ErrorCode.DEVICE_HAS_BEEN_BOUND);

		kwMerchantMeter.setStatus(EnumMerchantMeterStatus.VALID.getCode());
		kwMerchantMeterService.saveOrUpdate(kwMerchantMeter);

		return new ResultData();
    }

	@ApiOperation("解绑定设备")
	@PostMapping("unbind")
	@SystemControllerLog(description = "电表设备-解绑定设备")
	public Result unbind(@RequestBody KwMerchantMeter kwMerchantMeter) {

		if (kwMerchantMeter == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		if (kwMerchantMeter.getMerchantId() == null
				&& kwMerchantMeter.getMeterId() == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);

		if (kwMerchantMeter.getMeterId() != null) {
			kwMerchantMeterService.unbind(kwMerchantMeter.getMeterId());
		} else {
			kwMerchantMeterService.unbindBatch(kwMerchantMeter.getMerchantId());
		}

		return new ResultData();
	}


    @ApiOperation("商户电表列表")
    @GetMapping("getMechantMeter")
    @SystemControllerLog(description = "商户列表")
    public ResultData getMechantMeter() {
        return new ResultData(kwMerchantMeterService.getMerchantMeter(getTenantId()));
    }

}
