package com.sptg.estate.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.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.estate.api.dto.DeviceTestDto;
import com.sptg.estate.api.entity.DeviceTest;
import com.sptg.estate.biz.mapper.DeviceTestMapper;
import com.sptg.estate.biz.service.DeviceTestServiceInterface;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * title: ProjectServiceImpl
 * package: com.sptg.estate.biz.security
 * description: 设备检测-服务层实现层
 *
 *               
 *                     
 */
@Service
public class DeviceTestServiceImpl extends ServiceImpl<DeviceTestMapper, DeviceTest> implements DeviceTestServiceInterface {

    @Override
    public Page<DeviceTest> showList(DeviceTestDto dto) {
        Page<DeviceTest> page = dto.getPage();
        Wrapper<DeviceTest> wrapper = new EntityWrapper<DeviceTest>();
        //设备序列号
        String serialNumber = dto.getSerialNumber();
        if(StringUtils.isNotBlank(serialNumber)){
            wrapper.like("s_serial_number", serialNumber);
        }
        //楼盘名字
        String residentialName = dto.getResidentialName();
        if(StringUtils.isNotBlank(residentialName)){
            wrapper.like("s_residential_name", residentialName);
        }
        //小区id
        Long residentialId = dto.getResidentialId();
        if(residentialId!=null){
            wrapper.eq("l_residential_id", residentialId);
        }
        //检测人
        String createName = dto.getCreateName();
        if(StringUtils.isNotBlank(createName)){
            wrapper.like("s_create_name", createName);
        }
        //开始检测时间
        Date beginDate = dto.getBeginDate();
        if(beginDate!=null){
            wrapper.ge("t_create_date",beginDate);
        }
        //结束检测时间
        Date endDate = dto.getEndDate();
        if(endDate!=null){
            wrapper.le("idt.t_create_date",endDate);
        }
        //机构位置
        String orgPosition = dto.getOrgPosition();
        if(StringUtils.isNotBlank(orgPosition)){
            wrapper.like("s_org_position", orgPosition, SqlLike.RIGHT);
        }
        //小程序端条件查询
        if(StringUtils.isNotBlank(dto.getConditionName())){
            wrapper.like("s_serial_number", dto.getConditionName()).or().like("s_create_name",dto.getConditionName()).or().like("s_unit_name",dto.getConditionName()).or().like("s_install_path",dto.getConditionName());
        }
        //排序
        wrapper.orderBy("t_create_date",false);
        return this.selectPage(dto.getPage(),wrapper);
    }

    @Override
    public DeviceTest findById(Long id) {
        return this.selectById(id);
    }

    @Override
    public boolean checkState(DeviceTestDto dto) {
        if(dto==null||dto.getId()==null||dto.getCheckState()==null){
            return false;
        }
        //判断是否可审核
        DeviceTest currentDeviceTest = this.selectById(dto.getId());
        if(currentDeviceTest==null||currentDeviceTest.getCheckState()!=0){
            return false;
        }
        DeviceTest deviceTest=new DeviceTest();
        deviceTest.setId(dto.getId());
        if(dto.getCheckState()== OperationConstant.TWO){
            deviceTest.setCheckOpinion(dto.getCheckOpinion());
        }
        deviceTest.setCheckDate(new Date());
        deviceTest.setCheckAccountId(dto.getCheckAccountId());
        deviceTest.setCheckName(dto.getCheckName());
        deviceTest.setCheckState(dto.getCheckState());
        boolean result  = this.updateById(deviceTest);
        return result;
    }

    @Override
    public ResultBean<Integer> countByOrgPosition(String orgPosition) {
        Wrapper<DeviceTest> wrapper = new EntityWrapper<>();
        wrapper.like("s_org_position", orgPosition);
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectCount(wrapper));
    }

    @Override
    public ResultBean<DeviceTest> findListByPosition(DeviceTestDto dto) {
        Wrapper<DeviceTest> wrapper = new EntityWrapper<>();
        //机构结构
        wrapper.like("idt.s_org_position", dto.getOrgPosition());
        return new ResultBean<>(ResultEnum.SUCCESS,this.baseMapper.findListByPosition(wrapper));
    }

    @Override
    public ResultBean<DeviceTest> findBySerialNumber(String serialNumber) {
        Wrapper<DeviceTest> wrapper = new EntityWrapper<>();
        //序列号
        wrapper.eq("s_serial_number", serialNumber);
        // 排序
        wrapper.orderBy("createDate", false);
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectOne(wrapper));
    }

    @Override
    public ResultBean<DeviceTest> findByDeviceInstallationIds(List<Long> deviceInstallationIds) {
        Wrapper<DeviceTest> wrapper = new EntityWrapper<>();
        //设备安装记录id
        wrapper.in("l_device_installation_id", deviceInstallationIds);
        //审核通过的状态记录
        wrapper.eq("i_check_state", OperationConstant.ONE);
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectList(wrapper));
    }
}
