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.TimeStrategyDto;
import com.sptg.device.api.entity.TimeStrategy;
import com.sptg.device.api.feigns.TimeStrategyFeign;
import com.sptg.device.biz.service.TimeStrategyServiceInterface;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * title: TimeStrategyController
 * package: com.sptg.device.biz.controller
 * description: 时间策略-控制器
 *
 *                
 *               
 */
@RestController
@RequiredArgsConstructor
public class TimeStrategyController implements TimeStrategyFeign {
    private final TimeStrategyServiceInterface timeStrategyServiceInterface;

    @Override
    @PostMapping(value = "/time/strategy/pagingQuery")
    public ResultBean<Page<TimeStrategy>> pagingQuery(@RequestBody TimeStrategyDto dto) {
        Page<TimeStrategy> page=timeStrategyServiceInterface.showTimeStrategyList(dto);
        return new ResultBean<>(ResultEnum.SUCCESS,page);
    }

    @Override
    @GetMapping(value = "/time/strategy/selectById")
    public TimeStrategy selectById(@RequestBody Long id) {
        return this.timeStrategyServiceInterface.selectById(id);
    }

    @Override
    @PostMapping(value = "/time/strategy/addEntity")
    public ResultBean<Boolean> addEntity(@RequestBody TimeStrategyDto entity){
        boolean result=timeStrategyServiceInterface.addTimeStrategy(entity);
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @PostMapping(value = "/time/strategy/getEntityById")
    public  ResultBean<TimeStrategy> getEntityById(@RequestBody TimeStrategyDto dto) {
        TimeStrategy timeStrategy=timeStrategyServiceInterface.getTimeStrategyById(dto.getId());
        Assert.notNull(timeStrategy,"时间策略Id不存在");
        //  权限校验
        if (!timeStrategy.getOrgPosition().startsWith(dto.getOrgPosition())){
            return new ResultBean<>(ResultEnum.FORBIDDEN);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,timeStrategy);
    }

    @Override
    @PostMapping(value = "/time/strategy/editEntity")
    public ResultBean<Boolean> editEntity(@RequestBody TimeStrategyDto dto) {
        boolean result=timeStrategyServiceInterface.editTimeStrategy(dto);
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @PostMapping(value = "/time/strategy/deleteEntity")
    public ResultBean<Boolean> deleteEntity(@RequestBody TimeStrategyDto dto) {
        TimeStrategy timeStrategy=timeStrategyServiceInterface.selectById(dto.getId());
        Assert.notNull(timeStrategy,"时间策略Id不存在");
        //  权限校验
        if (!timeStrategy.getOrgPosition().startsWith(dto.getOrgPosition())){
            return new ResultBean<>(ResultEnum.FORBIDDEN);
        }
        //删除
        timeStrategy.setStatus(OperationConstant.MINUS_ONE);
        boolean result=timeStrategyServiceInterface.updateById(timeStrategy);
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @PostMapping(value = "/time/strategy/getEntityList")
    public List<TimeStrategy> getEntityList(@RequestBody TimeStrategyDto dto) {
        Wrapper<TimeStrategy> wrapper = new EntityWrapper<TimeStrategy>();
        wrapper.like("s_org_position",dto.getOrgPosition(), SqlLike.RIGHT);
        //  判断是否根据名称模糊查询
        if (!StringUtils.isEmpty(dto.getName())){
            wrapper.like("s_name",dto.getName(), SqlLike.RIGHT);

        }
        wrapper.eq("i_status",OperationConstant.ZERO);
        return this.timeStrategyServiceInterface.selectList(wrapper);
    }
}
