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.ProjectDto;
import com.sptg.estate.api.entity.Contract;
import com.sptg.estate.api.entity.ContractImg;
import com.sptg.estate.api.entity.Project;
import com.sptg.estate.api.entity.Residential;
import com.sptg.estate.biz.mapper.ProjectMapper;
import com.sptg.estate.biz.service.ContractImgServiceInterface;
import com.sptg.estate.biz.service.ContractServiceInterface;
import com.sptg.estate.biz.service.ProjectServiceInterface;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * title: ProjectServiceImpl
 * package: com.sptg.estate.biz.security
 * description: 实施项目-服务层实现层
 *
 *               
 *                     
 */
@Service
@RequiredArgsConstructor
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectServiceInterface{

    @Autowired
    @Lazy
    private ContractServiceInterface contractServiceInterface;

    @Override
    public ResultBean<Page<Project>> showList(ProjectDto projectDto) {
        Page<Project> page = projectDto.getPage();
        //排序字段 时间降序
        page.setOrderByField("p.t_create_date");
        page.setAsc(false);
        Wrapper<Project> wrapper = new EntityWrapper<>();
        wrapper.eq("p.i_status", OperationConstant.ZERO);
        //机构结构
        wrapper.like("p.s_org_position", projectDto.getOrgPosition());
        //楼盘名字
        if(StringUtils.isNotBlank(projectDto.getResidentialName())){
            wrapper.like("p.s_residential_name", projectDto.getResidentialName());
        }
        //合同编号
        if(StringUtils.isNotBlank(projectDto.getContractNo())){
            wrapper.like("c.s_contract_no", projectDto.getContractNo());
        }
        //物业管理方
        if(StringUtils.isNotBlank(projectDto.getPropertyName())){
            wrapper.like("c.s_property_name",projectDto.getPropertyName());
        }
        //综合服务方
        if(StringUtils.isNotBlank(projectDto.getIntegratedServiceName())){
            wrapper.like("s.s_integrated_service_name",projectDto.getIntegratedServiceName());
        }
        //xml文件sql查询
        page.setRecords(this.baseMapper.showList(wrapper,page));
        return new ResultBean<>(ResultEnum.SUCCESS, page);
    }

    @Override
    public ResultBean<Page<Project>> showListInstallation(ProjectDto projectDto) {
        Page<Project> page = projectDto.getPage();
        //排序字段 时间降序
        page.setOrderByField("p.t_create_date");
        page.setAsc(false);
        Wrapper<Project> wrapper = new EntityWrapper<>();
        wrapper.eq("p.i_status", OperationConstant.ZERO);
        //机构结构
        wrapper.like("p.s_org_position", projectDto.getOrgPosition());
        //楼盘名字
        if(StringUtils.isNotBlank(projectDto.getResidentialName())){
            wrapper.like("p.s_residential_name", projectDto.getResidentialName());
        }
        //xml文件sql查询
        page.setRecords(this.baseMapper.showListInstallation(wrapper,page));
        return new ResultBean<>(ResultEnum.SUCCESS, page);
    }

    @Override
    public ResultBean<Page<Project>> showListTest(ProjectDto projectDto) {
        Page<Project> page = projectDto.getPage();
        //排序字段 时间降序
        page.setOrderByField("p.t_create_date");
        page.setAsc(false);
        Wrapper<Project> wrapper = new EntityWrapper<>();
        wrapper.eq("p.i_status", OperationConstant.ZERO);
        //机构结构
        wrapper.like("p.s_org_position", projectDto.getOrgPosition());
        //楼盘名字
        if(StringUtils.isNotBlank(projectDto.getResidentialName())){
            wrapper.like("p.s_residential_name", projectDto.getResidentialName());
        }
        //xml文件sql查询
        page.setRecords(this.baseMapper.showListTest(wrapper,page));
        return new ResultBean<>(ResultEnum.SUCCESS, page);
    }

    @Override
    public ResultBean<Project> findByResidentialId(Long residentialId) {
        Wrapper<Project> wrapper = new EntityWrapper<>();
        wrapper.eq("l_residential_id",residentialId);
        return new ResultBean<>(ResultEnum.SUCCESS, selectOne(wrapper));
    }

    @Override
    public ResultBean<Boolean> countByResidentialId(Long residentialId) {
        Wrapper<Project> wrapper = new EntityWrapper<>();
        //小区id
        wrapper.eq("l_residential_id", residentialId);
        wrapper.eq("i_status", OperationConstant.ZERO);
        int count = this.selectCount(wrapper);
        if(count>0){
            return new ResultBean<>(ResultEnum.SUCCESS,true);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,false);
    }

    @Override
    public ResultBean<Project> findListByPosition(ProjectDto projectDto) {
        Wrapper<Project> wrapper = new EntityWrapper<>();
        wrapper.eq("p.i_status", OperationConstant.ZERO);
        //机构结构
        wrapper.like("p.s_org_position", projectDto.getOrgPosition());
        return new ResultBean<>(ResultEnum.SUCCESS,this.baseMapper.findListByPosition(wrapper));
    }

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

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public ResultBean<Boolean> insertProjectAndContract(ProjectDto dto) {
        if(this.countByResidentialId(dto.getResidentialId()).getData()){
            return new ResultBean<>(ResultEnum.SUCCESS,false);
        }else{
            Boolean result = this.insert(dto);
            if(result){
                Contract contract = dto.getContract();
                contract.setCreateDate(dto.getCreateDate());
                contract.setProjectId(dto.getId());
                ResultBean<Boolean> r = this.contractServiceInterface.editByProjectId(contract);
                result = r.getData();
            }
            return new ResultBean<>(ResultEnum.SUCCESS,result);
        }
    }

    @Override
    public List<Project> findListByXqIds(List<Long> idList) {
        Wrapper<Project> wrapper = new EntityWrapper<>();
        wrapper.in("l_residential_id",idList);
        wrapper.notIn("i_status", OperationConstant.MINUS_ONE);
        return this.selectList(wrapper);
    }
}
