package com.sptg.device.biz.controller;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.device.api.dto.DevicePathDto;
import com.sptg.device.api.entity.DevicePath;
import com.sptg.device.api.feigns.DevicePathFeign;
import com.sptg.device.biz.service.DevicePathServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * title: DevicePathController
 * package: com.sptg.device.biz.controller
 * description: 设备位置-控制器
 *
 *
 *
 */
@RestController
@RequiredArgsConstructor
public class DevicePathController implements DevicePathFeign {

    private final  DevicePathServiceInterface devicePathServiceInterface;

    @Override
    @PostMapping(value = "/devicePath/pagingQuery")
    public ResultBean<Page> pagingQuery(@RequestBody DevicePathDto dto) {
        Page<DevicePath> devicePathPage = this.devicePathServiceInterface.pagingQuery(dto);
        return new ResultBean<>(ResultEnum.SUCCESS,devicePathPage);
    }

    @Override
    @PostMapping(value = "/devicePath/addEntity")
    public ResultBean<Boolean> addEntity(@RequestBody DevicePathDto dto) {
        dto.setStatus(OperationConstant.ZERO);

        DevicePathDto devicePathDto = new DevicePathDto();
        devicePathDto.setName(dto.getName());
        devicePathDto.setXqId(dto.getXqId());
        devicePathDto.setOrgPosition(dto.getOrgPosition());
//        DevicePath devicePath = this.devicePathServiceInterface.selectOne(dto);
//        Assert.isNull(devicePath,"该位置已存在");
        Date date =new Date();
        dto.setCreateDate(date);
        dto.setUpdateDate(date);

        //生成设备位置编号
        //获取日期字符串
        String s = new SimpleDateFormat("yyMMdd").format(date);
        Wrapper<DevicePath> wrapper = new EntityWrapper<>();
        wrapper.like("s_code",s, SqlLike.RIGHT);
        //统计当天新增的公告条数
        int count = this.devicePathServiceInterface.selectCount(wrapper);
        String n=String.format("%02d",count);
        //公告编号的格式用yyMMdd+2位排序号前面补0
        dto.setCode(s+n);
        boolean result = this.devicePathServiceInterface.insert(dto);
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @PostMapping(value = "/devicePath/getEntityById")
    public DevicePath getEntityById(@RequestBody DevicePathDto dto){
        DevicePath devicePath = this.devicePathServiceInterface.selectById(dto.getId());
        Assert.notNull(devicePath,"设备位置不存在");
        //  权限校验
        if (!devicePath.getOrgPosition().startsWith(dto.getOrgPosition())){
            return null;
        }
        return devicePath;
    }

    @Override
    @GetMapping(value = "/devicePath/getEntityById/{id}")
    public DevicePath getEntityById(@PathVariable(value = "id")Long id) {
        return this.devicePathServiceInterface.selectById(id);
    }

    @Override
    @PostMapping(value = "/devicePath/editEntity")
    public ResultBean<Boolean> editEntity(@RequestBody DevicePathDto dto) {
        DevicePath devicePath = this.devicePathServiceInterface.selectById(dto.getId());
        Assert.notNull(devicePath,"设备位置不存在");
        DevicePathDto devicePathDto = new DevicePathDto();
        devicePathDto.setName(dto.getName());
        devicePathDto.setXqId(dto.getXqId());
        devicePathDto.setOrgPosition(dto.getOrgPosition());
        DevicePath devicePath1 = this.devicePathServiceInterface.selectOne(dto);
        if (devicePath1 != null){
            Boolean b = devicePath.getId().equals(devicePath1.getId());
            Assert.isTrue(b,"该位置已存在");
        }
        //  权限校验
        if (!devicePath.getOrgPosition().startsWith(dto.getOrgPosition())){
            return new ResultBean<>(ResultEnum.FORBIDDEN);
        }
        devicePath.setName(dto.getName());
        devicePath.setXqId(dto.getXqId());
        devicePath.setXqName(dto.getXqName());
        devicePath.setType(dto.getType());
        devicePath.setPathUse(dto.getPathUse());
        devicePath.setUpdateDate(new Date());
        boolean result = this.devicePathServiceInterface.updateById(devicePath);
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @PostMapping(value = "/devicePath/deleteEntity")
    public ResultBean<Boolean> deleteEntity(@RequestBody DevicePathDto dto) {

        DevicePath devicePath = this.devicePathServiceInterface.selectById(dto.getId());
        Assert.notNull(devicePath,"时间策略Id不存在");
        //  权限校验
        if (!devicePath.getOrgPosition().startsWith(dto.getOrgPosition())){
            return new ResultBean<>(ResultEnum.FORBIDDEN);
        }
        //删除
        devicePath.setStatus(OperationConstant.MINUS_ONE);
        boolean result = this.devicePathServiceInterface.updateById(devicePath);
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @PostMapping(value = "/devicePath/getList")
    public List<DevicePath> getList(@RequestBody DevicePathDto dto) {
        return this.devicePathServiceInterface.getList(dto);
    }

    @Override
    @GetMapping(value = "/devicePath/getByOldId/{oldId}")
    public DevicePath getByOldId(@PathVariable(value = "oldId") String oldId) {
        Wrapper<DevicePath> wrapper = new EntityWrapper<>();
        wrapper.eq("old_id",oldId);
        return this.devicePathServiceInterface.selectOne(wrapper);
    }

    @Override
    @PostMapping(value = "/devicePath/updateBatchById")
    public Boolean updateBatchById(@RequestBody List<DevicePath> devicePathList){
        return this.devicePathServiceInterface.updateBatchById(devicePathList);
    }
}
