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.Contract;
import com.sptg.estate.api.entity.Project;
import com.sptg.estate.api.entity.ProjectBook;
import com.sptg.estate.biz.mapper.ProjectBookMapper;
import com.sptg.estate.biz.service.ProjectBookServiceInterface;
import com.sptg.estate.biz.service.ProjectServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * title: ProjectServiceImpl
 * package: com.sptg.estate.biz.security
 * description: 立项书-服务层实现层
 *
 *               
 *                     
 */
@Service
@RequiredArgsConstructor
public class ProjectBookServiceImpl extends ServiceImpl<ProjectBookMapper, ProjectBook> implements ProjectBookServiceInterface{

    private final ProjectServiceInterface projectServiceInterface;

    @Override
    public ResultBean<Boolean> insertByResidentialId(ProjectBook projectBook) {
        Boolean result = false;
        ResultBean<Project> project = this.projectServiceInterface.findByResidentialId(projectBook.getResidentialId());
        ProjectBook book = this.findByProjectId(project.getData().getId()).getData();
        //更新
        if(book!=null){
            projectBook.setId(book.getId());
            result = this.updateById(projectBook);
        }else{//新增
            projectBook.setProjectId(project.getData().getId());
            result = this.insert(projectBook);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }
    @Override
    public ResultBean<ProjectBook> findByProjectId(Long projectId){
        Wrapper<ProjectBook> wrapper = new EntityWrapper<>();
        wrapper.eq("l_project_id",projectId);
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectOne(wrapper));
    }

    @Override
    public ResultBean<Boolean> editByProjectId(ProjectBook projectBook){
        Boolean result = false;
        ResultBean<ProjectBook> book = this.findByProjectId(projectBook.getProjectId());
        //更新
        if(book.getCode()=="200" && book.getData()!=null){
            projectBook.setId(book.getData().getId());
            result = this.updateById(projectBook);
        }else{//新增
            result = this.insert(projectBook);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }
}
