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

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
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.EstateRepairDto;
import com.sptg.estate.api.entity.EstateRepair;
import com.sptg.estate.biz.mapper.EstateRepairMapper;
import com.sptg.estate.biz.service.EstateRepairServiceInterface;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

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

/**
 * title: EstateRepairServiceImpl
 * package: com.sptg.estate.biz.security.impl
 * description: 物业报修-服务层实现层
 *
 *
 *
 */
@Service
@AllArgsConstructor
public class EstateRepairServiceImpl extends ServiceImpl<EstateRepairMapper, EstateRepair> implements EstateRepairServiceInterface {



    @Override
    public Page<EstateRepair> showEstateRepairList(EstateRepairDto estateRepairDto) {
        Page<EstateRepair> page=estateRepairDto.getPage();
        EntityWrapper<EstateRepair> wrapper=new EntityWrapper<>();
        //报修位置
        String reportPosition=estateRepairDto.getReportPosition();
        //小区名称
        String residentialName=estateRepairDto.getResidentialName();
        //预约开始时间条件
        Date beginTime=estateRepairDto.getBeginTime();
        //预约结束时间条件
        Date endTime=estateRepairDto.getEndTime();
        //机构
        Long orgId=estateRepairDto.getOrgId();
        //根机构
        Long rootOrgId=estateRepairDto.getRootOrgId();
        //机构位置
        String orgPosition=estateRepairDto.getOrgPosition();
        Long memberId = estateRepairDto.getMemberId();

        Integer handleStatus = estateRepairDto.getHandleStatus();
        String repairNo = estateRepairDto.getRepairNo();
        Integer estimateStatus = estateRepairDto.getEstimateStatus();
        if(null != estimateStatus){
            wrapper.eq("i_estimate_status",estimateStatus);
        }
        if(memberId != null){
            wrapper.eq("l_member_id",memberId);
        }
        if(handleStatus != null){
            wrapper.eq("i_handle_status",handleStatus);
        }
        if(StringUtils.isNotBlank(repairNo)){
            wrapper.eq("s_repair_no",repairNo);
        }

        if(StringUtils.isNotBlank(reportPosition)) {
            wrapper.like("s_report_position", reportPosition);
        }
        if(StringUtils.isNotBlank(residentialName)){
            wrapper.like("l_residential_name",residentialName);
        }
        if(beginTime!=null && endTime!=null){
            wrapper.ge("t_begin_time",beginTime).and().le("t_end_time",endTime);
        }

        //正常状态
//        wrapper.eq("i_status",OperationConstant.ZERO);
        if(orgId!=null){
            wrapper.eq("l_org_id",orgId);
        }
        if(rootOrgId!=null){
            wrapper.eq("l_root_org_id",rootOrgId);
        }
        if(StringUtils.isNotBlank(orgPosition)){
            wrapper.like("s_org_position",orgPosition, SqlLike.RIGHT);
        }
        wrapper.eq("i_status",OperationConstant.ZERO);
        //排序字段 时间排序
        wrapper.orderBy("t_create_time",false);
        //分页查询
        List<EstateRepair> estateRepairList=this.baseMapper.showEstateRepairList(page,wrapper);
        page.setRecords(estateRepairList);
        return page;
    }
    @Override
    public List<EstateRepair> getList(EstateRepairDto estateRepairDto) {
        EntityWrapper<EstateRepair> wrapper=new EntityWrapper<EstateRepair>();
        //报修位置
        String reportPosition=estateRepairDto.getReportPosition();
        //小区名称
        String residentialName=estateRepairDto.getResidentialName();
        //预约开始时间条件
        Date beginTime=estateRepairDto.getBeginTime();
        //预约结束时间条件
        Date endTime=estateRepairDto.getEndTime();
        //机构
        Long orgId=estateRepairDto.getOrgId();
        //根机构
        Long rootOrgId=estateRepairDto.getRootOrgId();
        //机构位置
        String orgPosition=estateRepairDto.getOrgPosition();

        String repairNo = estateRepairDto.getRepairNo();

        Long memberId = estateRepairDto.getMemberId();

        Integer handleStatus = estateRepairDto.getHandleStatus();
        if(memberId != null){
            wrapper.eq("l_member_id",memberId);
        }
        if(handleStatus != null){
            wrapper.eq("i_handle_status",handleStatus);
        }
        if(StringUtils.isNotBlank(repairNo)){
            wrapper.eq("s_repair_no",repairNo);
        }
        if(StringUtils.isNotBlank(reportPosition)) {
            wrapper.like("s_report_position", reportPosition);
        }
        if(StringUtils.isNotBlank(residentialName)){
            wrapper.like("l_residential_name",residentialName);
        }
        if(beginTime!=null){
            wrapper.ge("t_begin_time",beginTime);
        }
        if(endTime!=null){
            wrapper.le("t_end_time",endTime);
        }
        //正常状态
//        wrapper.eq("i_status",OperationConstant.ZERO);
        if(orgId!=null){
            wrapper.eq("l_org_id",orgId);
        }
        if(rootOrgId!=null){
            wrapper.eq("l_root_org_id",rootOrgId);
        }
        if(StringUtils.isNotBlank(orgPosition)){
            wrapper.like("s_org_position",orgPosition, SqlLike.RIGHT);
        }
        wrapper.eq("i_status",OperationConstant.ZERO);
        //排序字段 时间排序
        wrapper.orderBy("t_create_time",false);
        //查询
        if(estateRepairDto.getPage()!=null){
            return  this.baseMapper.showEstateRepairList(estateRepairDto.getPage(),wrapper);
        }else{
            return this.selectList(wrapper);
        }

    }
    @Override
    public EstateRepair addOrEditEstateRepair(EstateRepair estateRepair) {
        //插入更新物业报修
        this.insertOrUpdate(estateRepair);
        return estateRepair;
    }
    @Override
    public EstateRepair findEstateRepairById(long id){
        EstateRepair estateRepair = this.selectById(id);
        if(estateRepair.getStatus() == 0){
            return estateRepair;
        }else{
            return null;
        }

    }

    @Override
    public boolean deleteEstateRepairById(long id){
        EstateRepair estateRepair = this.selectById(id);
        //删除
        estateRepair.setStatus(OperationConstant.MINUS_ONE);
        boolean result=this.updateById(estateRepair);
        return result;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultBean<Boolean> deleteEstateRepairList(EstateRepairDto estateRepairDto){
        List<Long> ids = estateRepairDto.getIds();
        for(Long id : ids){
            EstateRepair estateRepair = this.selectById(id);
            Assert.notNull(estateRepair,"记录不存在,请核实后重新操作");
            estateRepair.setStatus(OperationConstant.MINUS_ONE);
            this.updateById(estateRepair);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,true);
    }
}
