package com.sptg.device.biz.service.impl;

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.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.beans.PageBean;
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.DeviceTemplateDto;
import com.sptg.device.api.entity.DeviceTemplate;
import com.sptg.device.biz.mapper.DeviceTemplateMapper;
import com.sptg.device.biz.service.DeviceTemplateServiceInterface;
import com.sptg.estate.api.dto.ResidentialDto;
import com.sptg.estate.api.entity.Residential;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

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

@Service
@AllArgsConstructor
public class DeviceTemplateServiceImpl extends ServiceImpl<DeviceTemplateMapper, DeviceTemplate> implements DeviceTemplateServiceInterface {

    @Override
    public ResultBean<Page<DeviceTemplate>> pagingQuery(DeviceTemplateDto dto) {
        Wrapper<DeviceTemplate> wrapper = new EntityWrapper<>();
        wrapper.eq("i_status", OperationConstant.ZERO);
        //排序
        if (dto.getSortField() != null && dto.getSortOrder() != null){
            wrapper.orderBy(dto.getSortField(),dto.getSortOrder().equals("asc"));
        }else {
            wrapper.orderBy("t_create_date",false);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectPage(dto.getPage(),wrapper));
    }

    @Override
    public ResultBean<Boolean> addDeviceTemplate(DeviceTemplateDto deviceTemplateDto) {
        DeviceTemplate deviceTemplate = new DeviceTemplate();
        deviceTemplate.setTypeCode(deviceTemplateDto.getTypeCode());
        deviceTemplate.setDeviceFactory(deviceTemplateDto.getDeviceFactory());
        List<DeviceTemplate> list = this.getListByCondition(deviceTemplateDto);
        Assert.isTrue(list == null || list.size() == 0, "相同型号和厂家的模板配置已存在,请勿重复创建");
        BeanUtils.copyProperties(deviceTemplateDto,deviceTemplate);
        deviceTemplate.setCreateDate(new Date());
        deviceTemplate.setStatus(OperationConstant.ZERO);
        return new ResultBean<>(ResultEnum.SUCCESS,this.insert(deviceTemplate));
    }

    @Override
    public ResultBean<Boolean> updateDeviceTemplate(DeviceTemplateDto deviceTemplateDto) {
        DeviceTemplate deviceTemplate = this.selectById(deviceTemplateDto.getId());
        Assert.notNull(deviceTemplate,"修改失败，该配置不存在");
        BeanUtils.copyProperties(deviceTemplateDto,deviceTemplate);
        deviceTemplate.setUpdateDate(new Date());
        return new ResultBean<>(ResultEnum.SUCCESS,this.updateById(deviceTemplate));
    }

    @Override
    public ResultBean<Boolean> deleteDeviceTemplate(DeviceTemplateDto deviceTemplateDto) {
        DeviceTemplate deviceTemplate = this.selectById(deviceTemplateDto.getId());
        Assert.notNull(deviceTemplate,"删除失败，该配置不存在");
        deviceTemplate.setStatus(OperationConstant.MINUS_ONE);
        return new ResultBean<>(ResultEnum.SUCCESS,this.updateById(deviceTemplate));
    }

    @Override
    public DeviceTemplate getOne(DeviceTemplateDto deviceTemplateDto) {
        Wrapper<DeviceTemplate> wrapper = new EntityWrapper<>();
        wrapper.eq("i_status",OperationConstant.ZERO);
        if (deviceTemplateDto.getDeviceFactory() != null){
            wrapper.eq("s_device_factory",deviceTemplateDto.getDeviceFactory());
        }
        if (deviceTemplateDto.getTypeCode() != null){
            wrapper.eq("s_type_code",deviceTemplateDto.getTypeCode());
        }
        return this.selectOne(wrapper) ;
    }

    @Override
    public List<DeviceTemplate> getListByCondition(DeviceTemplateDto deviceTemplateDto) {
        Wrapper<DeviceTemplate> wrapper = new EntityWrapper<>();
        wrapper.eq("i_status",OperationConstant.ZERO);
        if (StringUtils.isNotBlank(deviceTemplateDto.getDeviceFactory())){
            wrapper.eq("s_device_factory",deviceTemplateDto.getDeviceFactory());
        }
        if (deviceTemplateDto.getTypeCode() != null){
            wrapper.like("s_type_code",deviceTemplateDto.getTypeCode(), SqlLike.DEFAULT);
        }
        if (deviceTemplateDto.getIsSelect()){
            wrapper.last("limit 10");
        }
        return this.selectList(wrapper);
    }

    @Override
    public List<DeviceTemplate> getListByLikeTypeCode(DeviceTemplateDto deviceTemplateDto) {
        Wrapper<DeviceTemplate> wrapper = new EntityWrapper<>();
        wrapper.eq("i_status",OperationConstant.ZERO);
        if (StringUtils.isNotBlank(deviceTemplateDto.getDeviceFactory())){
            wrapper.eq("s_device_factory",deviceTemplateDto.getDeviceFactory());
        }
        if (deviceTemplateDto.getTypeCode() != null){
            wrapper.like("s_type_code",deviceTemplateDto.getTypeCode(), SqlLike.DEFAULT);
        }
        if (deviceTemplateDto.getIsSelect()){
            wrapper.last("limit 10");
        }
        return this.selectList(wrapper);
    }
}
