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.AcceptanceDto;
import com.sptg.estate.api.entity.Acceptance;
import com.sptg.estate.biz.mapper.AcceptanceMapper;
import com.sptg.estate.biz.service.AcceptanceServiceInterface;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * title: ProjectServiceImpl
 * package: com.sptg.estate.biz.security
 * description: 竣工验收-服务层实现层
 *
 *               
 *                     
 */
@Service
public class AcceptanceServiceImpl extends ServiceImpl<AcceptanceMapper, Acceptance> implements AcceptanceServiceInterface {


    @Override
    public ResultBean<Boolean> countByProjectId(Long projectId) {
        Wrapper<Acceptance> wrapper = new EntityWrapper<>();
        //项目id
        wrapper.eq("l_project_id", projectId);
        int count = this.selectCount(wrapper);
        if(count>0){
            return new ResultBean<>(ResultEnum.SUCCESS,true);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,false);
    }

    @Override
    public ResultBean<Boolean> insertEntity(AcceptanceDto dto) {
        //一个项目只能有一条竣工验收记录
        if(!this.countByProjectId(dto.getProjectId()).getData()){
            return new ResultBean<>(ResultEnum.SUCCESS,this.insert(dto));
        }
        return new ResultBean<>(ResultEnum.SUCCESS,false);
    }

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

    @Override
    public Page<Acceptance> showList(AcceptanceDto dto) {
        Page<Acceptance> page = dto.getPage();
        Wrapper<Acceptance> 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.like("s_create_name", dto.getCreateName());
        }
        //排序
        wrapper.orderBy("t_create_date",false);
        return this.selectPage(page, wrapper);
    }

    @Override
    public ResultBean<Boolean> checkState(AcceptanceDto dto) {
        if(dto==null||dto.getId()==null||dto.getCheckState()==null){
            return new ResultBean<>(ResultEnum.SUCCESS,false);
        }
        //判断是否可审核
        Acceptance acceptance = this.selectById(dto.getId());
        if(acceptance==null || acceptance.getCheckState()==OperationConstant.ONE){
            return new ResultBean<>(ResultEnum.SUCCESS,false);
        }
        if(dto.getCheckState()== OperationConstant.TWO && StringUtils.isBlank(dto.getCheckOpinion())){
            return new ResultBean<>(ResultEnum.SUCCESS,false);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,this.updateById(dto));
    }
}
