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

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.estate.api.entity.EnvironmentalReview;
import com.sptg.estate.api.entity.InstallationReview;
import com.sptg.estate.api.entity.PositionRecord;
import com.sptg.estate.biz.mapper.InstallationReviewMapper;
import com.sptg.estate.biz.service.EnvironmentalReviewServiceInterface;
import com.sptg.estate.biz.service.InstallationReviewServiceInterface;
import com.sptg.estate.biz.service.PositionRecordServiceInterface;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * title: ProjectServiceImpl
 * package: com.sptg.estate.biz.security
 * description: 安装环境评审-服务层实现层
 *
 *               
 *                     
 */
@Service
@AllArgsConstructor
public class InstallationReviewServiceImpl extends ServiceImpl<InstallationReviewMapper, InstallationReview> implements InstallationReviewServiceInterface {

    private final EnvironmentalReviewServiceInterface environmentalReviewServiceInterface;
    private final PositionRecordServiceInterface positionRecordServiceInterface;

    @Override
    public List<InstallationReview> getListByPositionId(long positionId){
        List<InstallationReview> installationReviews = this.baseMapper.getListByPositionId(positionId);
        return installationReviews;
    }

    @Override
    public ResultBean<Boolean> countByPositionId(Long positionId) {
        Wrapper<InstallationReview> wrapper = new EntityWrapper<>();
        //定位id
        wrapper.eq("l_position_id", positionId);
        int count = this.selectCount(wrapper);
        if(count>0){
            return new ResultBean<>(ResultEnum.SUCCESS,true);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,false);
    }

    @Override
    public ResultBean<InstallationReview> findByPositionRecordId(Long positionRecordId) {
        Wrapper<InstallationReview> wrapper = new EntityWrapper<>();
        //定位记录id
        wrapper.eq("l_position_record_id", positionRecordId);
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectOne(wrapper));
    }
    private ResultBean<Boolean> countByPositionRecordId(Long positionRecordId) {
        Wrapper<InstallationReview> wrapper = new EntityWrapper<>();
        //定位id
        wrapper.eq("l_position_record_id", positionRecordId);
        int count = this.selectCount(wrapper);
        if(count>0){
            return new ResultBean<>(ResultEnum.SUCCESS,true);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,false);
    }
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public ResultBean<Boolean> insertOrUpdateEntity(InstallationReview installationReview) {
        //新增
        if(installationReview.getId()==null){
            //一条定位记录对应一条安装评审记录
            if(this.countByPositionRecordId(installationReview.getPositionRecordId()).getData()){
                return new ResultBean<>(ResultEnum.SUCCESS, false);
            }
            EnvironmentalReview environmentalReview = this.environmentalReviewServiceInterface.selectById(installationReview.getEnvironmentalReviewId());
            PositionRecord positionRecord = this.positionRecordServiceInterface.selectById(installationReview.getPositionRecordId());
            Integer deviceType = positionRecord.getDeviceType();
            //设备类型(门禁机中的类型) 0-立柱机  1-壁挂机 2-雨罩壁挂机
            EnvironmentalReview update = new EnvironmentalReview();
            update.setId(environmentalReview.getId());
            if(deviceType==0){
                Integer pillar = environmentalReview.getPillar();
                if(pillar!=null){
                    pillar = pillar+1;
                }else{
                    pillar = 1;
                }
                update.setPillar(pillar);
            }else if(deviceType==1){
                Integer wallHung = environmentalReview.getWallHung();
                if(wallHung!=null){
                    wallHung = wallHung+1;
                }else{
                    wallHung = 1;
                }
                update.setWallHung(wallHung);
            }else if(deviceType==2){
                Integer rainCover = environmentalReview.getRainCover();
                if(rainCover!=null){
                    rainCover = rainCover+1;
                }else{
                    rainCover = 1;
                }
                update.setRainCover(rainCover);
            }
            this.environmentalReviewServiceInterface.updateById(update);
            return new ResultBean<>(ResultEnum.SUCCESS, this.insert(installationReview));
        }else{
            return new ResultBean<>(ResultEnum.SUCCESS, this.updateById(installationReview));
        }
    }

    @Override
    public ResultBean<InstallationReview> findByEnvironmentalReviewId(Long environmentalReviewId) {
        Wrapper<InstallationReview> wrapper = new EntityWrapper<>();
        //定位记录id
        wrapper.eq("ir.l_environmental_review_id", environmentalReviewId);
        return new ResultBean<>(ResultEnum.SUCCESS,this.baseMapper.findByEnvironmentalReviewId(wrapper));
    }
}
