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

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.EnvironmentalReviewDto;
import com.sptg.estate.api.dto.PositionDto;
import com.sptg.estate.api.entity.EnvironmentalReview;
import com.sptg.estate.api.entity.Position;
import com.sptg.estate.biz.mapper.EnvironmentalReviewMapper;
import com.sptg.estate.biz.service.EnvironmentalReviewServiceInterface;
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 EnvironmentalReviewImpl extends ServiceImpl<EnvironmentalReviewMapper, EnvironmentalReview> implements EnvironmentalReviewServiceInterface {

    @Override
    public List<EnvironmentalReview> getListByPositionId(long positionId){
        Wrapper<EnvironmentalReview> wrapper=new EntityWrapper<EnvironmentalReview>();
        wrapper.eq("l_position_id",positionId);
        List<EnvironmentalReview> environmentalReviews = this.selectList(wrapper);
        return environmentalReviews;
    }

    @Override
    public ResultBean<Boolean> countByPositionId(Long positionId) {
        Wrapper<EnvironmentalReview> 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<EnvironmentalReview> findByPositionId(Long positionId) {
        Wrapper<EnvironmentalReview> wrapper = new EntityWrapper<>();
        //定位记录id
        wrapper.eq("l_position_id", positionId);
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectOne(wrapper));
    }

    @Override
    public ResultBean<Page<EnvironmentalReview>> showList(EnvironmentalReviewDto dto) {
        Page<EnvironmentalReview> page = dto.getPage();
        Wrapper<EnvironmentalReview> wrapper = new EntityWrapper<>();
        if(StringUtils.isNotBlank(dto.getResidentialName())){
            wrapper.like("s_residential_name",dto.getResidentialName());
        }
        //机构位置
        wrapper.like("s_org_position",dto.getOrgPosition());
        if(dto.getCheckState()!=null){
            wrapper.eq("i_check_state", dto.getCheckState());
        }
        if(StringUtils.isNotBlank(dto.getCreateName())){
            wrapper.eq("s_create_name", dto.getCreateName());
        }
        //排序
        wrapper.orderBy("t_create_date",false);
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectPage(page, wrapper));
    }

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

    @Override
    public ResultBean<Boolean> checkState(EnvironmentalReviewDto dto) {
        return new ResultBean<>(ResultEnum.SUCCESS,this.updateById(dto));
    }

    @Override
    public ResultBean<Boolean> insertOrUpdateEntity(EnvironmentalReview environmentalReview){
        if(this.countByPositionId(environmentalReview.getPositionId()).getData()){
            return new ResultBean<>(ResultEnum.SUCCESS,false);
        }else{
            return new ResultBean<>(ResultEnum.SUCCESS,this.insertOrUpdate(environmentalReview));
        }
    }

}
