package com.nantian.cloud.dbcapp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.dbcapp.entity.ConfigureGaugeRelevance;
import com.nantian.cloud.dbcapp.service.ConfigureGaugeRelevanceService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.lettuce.core.dynamic.annotation.Param;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author suixin
 * @date 2023/5/16
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/configureGaugeRelevance")
public class ConfigureGaugeRelevanceController {

    @Autowired
    private ConfigureGaugeRelevanceService configureGaugeRelevanceService;


    @RequestMapping("/listState")
    @SysLog("计量管理-计量器具关联勾选状态查询")
    @ApiOperation(value = "计量器具关联勾选状态查询", notes = "计量器具关联勾选状态查询")
    public R listState(Page page, String epc, String dataCode, String type){
        return R.ok(configureGaugeRelevanceService.listState(page, epc, dataCode, type));
    }

    @RequestMapping("/getList")
    @SysLog("计量管理-计量器具与采集配置项关联审核列表查询")
    @ApiOperation(value = "计量器具与采集配置项关联查询", notes = "计量器具与采集配置项关联查询")
    public R getList(Page page, String epc, String dataCode, String auditStatus){
        return R.ok(configureGaugeRelevanceService.getList(page, epc, dataCode, auditStatus));
    }

    @RequestMapping("/listGroup")
    @SysLog("计量管理-计量器具与采集配置项关联审核列表查询(按照社会信用代码、采集编码)")
    @ApiOperation(value = "计量器具与采集配置项关联查询", notes = "计量器具与采集配置项关联查询")
    public R listGroup(Page page, String epc, String dataCode, String auditStatus){
        return R.ok(configureGaugeRelevanceService.listGroupByEpcAndDataCode(page, epc, dataCode, auditStatus));
    }

    @RequestMapping("/relevanceState")
    @SysLog("计量管理-采集点关联状态查询")
    @ApiOperation(value = "采集点关联状态查询-是否已被关联", notes = "采集点关联状态查询-是否已被关联")
    public R relevanceState(String epc){
        return R.ok(configureGaugeRelevanceService.relevanceState(epc));
    }

    @RequestMapping("/listGauges")
    @SysLog("计量管理-根据关联关系查询计量器具")
    @ApiOperation(value = "根据关联关系查询计量器具", notes = "根据关联关系查询计量器具")
    public R listGauges(Page page, String epc, String dataCode){
        return R.ok(configureGaugeRelevanceService.getGaugeByRelevance(page,epc,dataCode));
    }


    @RequestMapping("/listConfigs")
    @SysLog("计量管理-根据关联关系查询采集配置项")
    @ApiOperation(value = "根据关联关系查询采集配置项", notes = "根据关联关系查询采集配置项")
    public R listConfigs(Page page, Long gaugeId){
        return R.ok(configureGaugeRelevanceService.getConfigByRelevance(page, gaugeId));
    }


    @RequestMapping("/save")
    @SysLog("计量管理-计量器具与采集配置项关联新增")
    @ApiOperation(value = "计量器具与采集配置项关联关系新增", notes = "计量器具与采集配置项关联关系新增")
    public R save(@RequestBody List<ConfigureGaugeRelevance> configureGaugeRelevances){
        ScUser user = SecurityUtils.getUser();
        String epc = null; String dataCode = null;
        for (ConfigureGaugeRelevance configureGaugeRelevance : configureGaugeRelevances){
            configureGaugeRelevance.setId(null);
            configureGaugeRelevance.setCreator( user.getUsername());
            epc = configureGaugeRelevance.getEnterpriseCode();
            dataCode = configureGaugeRelevance.getDataCode();
        }
        //将以前的该采集点的关联关系状态置为删除
        configureGaugeRelevanceService.updateDeleteByEpcAndDataCode(epc,dataCode);
        if (configureGaugeRelevanceService.saveBatch(configureGaugeRelevances)){
            return R.ok("保存成功！");
        }else {
            return R.failed("保存失败！");
        }
    }


    @RequestMapping("/audit")
    @SysLog("计量管理-计量器具与采集配置项关联审核")
    @ApiOperation(value = "计量器具与采集配置项关联关系审核", notes = "计量器具与采集配置项关联关系审核")
    public R audit(@RequestBody List<ConfigureGaugeRelevance> configureGaugeRelevances){
        ScUser user = SecurityUtils.getUser();
        for (ConfigureGaugeRelevance configureGaugeRelevance : configureGaugeRelevances){
            //审核状态：0-未审核，1-审核通过，2-审核不通过
            configureGaugeRelevance.setGmtAudit(new Date());
            configureGaugeRelevance.setAuditor(user.getUsername());
        }
        if ( configureGaugeRelevanceService.saveOrUpdateBatch(configureGaugeRelevances)){
            return R.ok("审核成功！");
        }else {
            return R.failed("审核失败");
        }
    }

    @RequestMapping("/auditGroup")
    @SysLog("计量管理-计量器具与采集配置项关联审核(按照社会信用代码及采集编码审核)")
    @ApiOperation(value = "计量器具与采集配置项关联关系审核（按照社会信用代码及采集编码审核）", notes = "计量器具与采集配置项关联关系审核(按照社会信用代码及采集编码审核)")
    public R auditGroup(@RequestBody List<ConfigureGaugeRelevance> configureGaugeRelevances){
        //审核状态：0-未审核，1-审核通过，2-审核不通过
        boolean flag = false;
        ScUser user = SecurityUtils.getUser();
        for (ConfigureGaugeRelevance configureGaugeRelevance : configureGaugeRelevances){
            //审核通过则更改计量器具等级为采集编码匹配的等级
            if ("1".equals(configureGaugeRelevance.getAuditStatus())){
                if (!configureGaugeRelevanceService.updateMeterLevel(configureGaugeRelevance.getEnterpriseCode(), configureGaugeRelevance.getDataCode())){
                    break;
                }
            }
            flag = configureGaugeRelevanceService.auditGroup(configureGaugeRelevance.getEnterpriseCode(),configureGaugeRelevance.getDataCode(), configureGaugeRelevance.getAuditStatus(), configureGaugeRelevance.getAuditDescription(),user.getUsername());
            if (!flag){
                break;
            }
        }
        if (flag){
            return R.ok("审核成功！");
        }else {
            return R.failed("审核失败");
        }
    }

    @RequestMapping("/delete")
    @SysLog("计量管理-计量器具与采集配置项关联审核")
    @ApiOperation(value = "计量器具与采集配置项关联关系审核", notes = "计量器具与采集配置项关联关系审核")
    public R delete(@RequestBody List<Long> ids){
        if (configureGaugeRelevanceService.updateDelete(ids)){
            return R.ok("删除成功");
        }else {
            return R.failed("删除失败");
        }
    }
}
