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.dto.BaseUser;
import com.sptg.estate.api.entity.Contract;
import com.sptg.estate.api.entity.ContractImg;
import com.sptg.estate.api.entity.IntegratedService;
import com.sptg.estate.api.entity.Project;
import com.sptg.estate.biz.mapper.ContractMapper;
import com.sptg.estate.biz.service.ContractImgServiceInterface;
import com.sptg.estate.biz.service.ContractServiceInterface;
import com.sptg.estate.biz.service.IntegratedServiceInterface;
import com.sptg.estate.biz.service.ProjectServiceInterface;
import lombok.RequiredArgsConstructor;
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 ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements ContractServiceInterface {

    private final ContractImgServiceInterface contractImgServiceInterface;
    private final IntegratedServiceInterface integratedServiceInterface;
    private final ProjectServiceInterface projectServiceInterface;

    @Override
    public ResultBean<Contract> findByProjectId(Long projectId) {
        Wrapper<Contract> wrapper = new EntityWrapper<>();
        wrapper.eq("l_project_id",projectId);
        Contract contract = selectOne(wrapper);
        if(contract!=null){
            List<ContractImg> list = this.contractImgServiceInterface.getListContractId(contract.getId());
            contract.setList(list);
        }
        return new ResultBean<>(ResultEnum.SUCCESS, contract);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public ResultBean<Boolean> insertByResidentialId(Contract contract){
        ResultBean<Project> project = this.projectServiceInterface.findByResidentialId(contract.getResidentialId());
        ResultBean<Contract> r = this.findByProjectId(project.getData().getId());
        contract.setProjectId(project.getData().getId());
        //更新
        Boolean result = insertOrEdit(contract, r);
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public ResultBean<Boolean> editByProjectId(Contract contract) {
        ResultBean<Contract> r = this.findByProjectId(contract.getProjectId());
        //更新
        Boolean result = insertOrEdit(contract, r);
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    private Boolean insertOrEdit(Contract contract, ResultBean<Contract> r) {
        Boolean result;
        if(r.getCode().equals("200") && r.getData()!=null){
            Contract c = r.getData();
            contract.setId(c.getId());
            result = this.updateById(contract);
            //删除关联的图片
            this.contractImgServiceInterface.deleteByContractId(c.getId());
            //修改了综合服务方，综合服务方的项目数发生变化
            if(!c.getIntegratedServiceId().equals(contract.getIntegratedServiceId())){
                //修改原来综合服务方关联的项目数 减1
                IntegratedService in = this.integratedServiceInterface.selectById(c.getIntegratedServiceId());
                IntegratedService i = new IntegratedService();
                i.setId(in.getId());
                i.setContractNumber(in.getContractNumber()-1);
                this.integratedServiceInterface.updateById(in);
                //修改新关联的综合分关联的项目数 加1
                in = this.integratedServiceInterface.selectById(contract.getIntegratedServiceId());
                i = new IntegratedService();
                i.setId(in.getId());
                i.setContractNumber(in.getContractNumber()+1);
                this.integratedServiceInterface.updateById(in);
            }
        }else {//新增
            result = this.insert(contract);
            //综合服务方关联的项目数+1
            if(result){
                IntegratedService i = this.integratedServiceInterface.selectById(contract.getIntegratedServiceId());
                if(i!=null){
                    IntegratedService in = new IntegratedService();
                    in.setId(i.getId());
                    in.setContractNumber(i.getContractNumber()+1);
                    result = this.integratedServiceInterface.updateById(in);
                }
            }
        }
        String[] imgUris = contract.getImgUri();
        if(result && imgUris!=null){
            ContractImg contractImg;
            List<ContractImg> list = new ArrayList<>();
            for(String imgUri : imgUris){
                contractImg = new ContractImg();
                contractImg.setContractId(contract.getId());
                contractImg.setImgUri(imgUri);
                list.add(contractImg);
            }
            if(list.size()>0){
                this.contractImgServiceInterface.addList(list);
            }
        }
        return result;
    }
}
