package com.yuchen.saas.park.service.service.renovation.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.entity.park.PmgBatParkInfo;
import com.yuchen.saas.park.api.entity.property.PropertyBuildingUnit;
import com.yuchen.saas.park.api.entity.property.PropertyBuildings;
import com.yuchen.saas.park.api.entity.renovation.*;
import com.yuchen.saas.park.api.vo.renovation.SaasParkPropertyHouseRenovationApplyVo;
import com.yuchen.saas.park.service.mapper.property.PropertyBuildingUnitMapper;
import com.yuchen.saas.park.service.mapper.property.PropertyBuildingsMapper;
import com.yuchen.saas.park.service.mapper.renovation.SaasParkPropertyHouseRenovationApplyMapper;
import com.yuchen.saas.park.service.mapper.renovation.SaasParkPropertyHouseRenovationConstructorInfoMapper;
import com.yuchen.saas.park.service.mapper.renovation.SaasParkPropertyHouseRenovationPersonInfoMapper;
import com.yuchen.saas.park.service.service.PmgBatParkInfoService;
import com.yuchen.saas.park.service.service.renovation.ISaasParkPropertyHouseRenovationApplyService;
import io.swagger.annotations.ApiModelProperty;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.BeanUtil;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 房源-装修人-施工方-关联申请表 服务实现类
 * </p>
 *
 * @author baichaojie
 * @since 2024-12-02
 */
@Service
public class SaasParkPropertyHouseRenovationApplyServiceImpl extends BaseServiceImpl<SaasParkPropertyHouseRenovationApplyMapper, SaasParkPropertyHouseRenovationApply> implements ISaasParkPropertyHouseRenovationApplyService {


    @Resource
    private PmgBatParkInfoService pmgBatParkInfoService;

    @Resource
    private PropertyBuildingsMapper propertyBuildingsMapper;

    @Resource
    private PropertyBuildingUnitMapper propertyBuildingUnitMapper;

    @Resource
    private SaasParkPropertyHouseRenovationPersonInfoMapper saasParkPropertyHouseRenovationPersonInfoMapper;

    @Resource
    private SaasParkPropertyHouseRenovationConstructorInfoMapper saasParkPropertyHouseRenovationConstructorInfoMapper;

    @Override
    public R<IPage<SaasParkPropertyHouseRenovationApplyVo>> getList(SaasParkPropertyHouseRenovationApplyAllDto saasParkPropertyHouseRenovationApplyDto, Query query) {

        Page<SaasParkPropertyHouseRenovationApply> page = new Page<>(query.getCurrent(), query.getSize());
        //分页查询
        LambdaQueryWrapper<SaasParkPropertyHouseRenovationApply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Func.isNotEmpty(saasParkPropertyHouseRenovationApplyDto.getParkId()), SaasParkPropertyHouseRenovationApply::getParkId, saasParkPropertyHouseRenovationApplyDto.getParkId())
                .orderByDesc(SaasParkPropertyHouseRenovationApply::getCreateTime);
        IPage<SaasParkPropertyHouseRenovationApply> pages = this.page(page, lambdaQueryWrapper);

        List<SaasParkPropertyHouseRenovationApply> saasParkPropertyHouseRenovationApplyList = pages.getRecords();

        if (saasParkPropertyHouseRenovationApplyList.isEmpty()) {
            return R.data(Condition.getPage(query));
        }
        //组装返回response
        IPage<SaasParkPropertyHouseRenovationApplyVo> pageVo = new Page(pages.getCurrent(), pages.getSize(), pages.getTotal());
        List<SaasParkPropertyHouseRenovationApplyVo> records = this.listVO(pages.getRecords());

        pageVo.setRecords(records);
        return R.data(pageVo);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean submitApply(SaasParkPropertyHouseRenovationApplyDto saasParkPropertyHouseRenovationApplyDto) {
        SaasParkPropertyHouseRenovationApply saasParkPropertyHouseRenovationApply = new SaasParkPropertyHouseRenovationApply();
        BeanUtil.copyProperties(saasParkPropertyHouseRenovationApplyDto, saasParkPropertyHouseRenovationApply);
        if (saasParkPropertyHouseRenovationApplyDto.getId() == null) {
            saasParkPropertyHouseRenovationApply.setCreateTime(new Date());
            saasParkPropertyHouseRenovationApply.setApplyStatus(0);
            saasParkPropertyHouseRenovationApply.setSure(0);
        }
        SaasParkPropertyHouseRenovationPersonInfo saasParkPropertyHouseRenovationPersonInfo = new SaasParkPropertyHouseRenovationPersonInfo();
        saasParkPropertyHouseRenovationPersonInfo = saasParkPropertyHouseRenovationApplyDto.getSaasParkPropertyHouseRenovationPersonInfo();
        saasParkPropertyHouseRenovationPersonInfo.setId(null);
        saasParkPropertyHouseRenovationPersonInfoMapper.insert(saasParkPropertyHouseRenovationPersonInfo);

        saasParkPropertyHouseRenovationApply.setRelativePersonId(saasParkPropertyHouseRenovationPersonInfo.getId());

        SaasParkPropertyHouseRenovationConstructorInfo saasParkPropertyHouseRenovationConstructorInfo = new SaasParkPropertyHouseRenovationConstructorInfo();
        saasParkPropertyHouseRenovationConstructorInfo = saasParkPropertyHouseRenovationApplyDto.getSaasParkPropertyHouseRenovationConstructorInfo();
        saasParkPropertyHouseRenovationConstructorInfo.setId(null);
        saasParkPropertyHouseRenovationConstructorInfoMapper.insert(saasParkPropertyHouseRenovationConstructorInfo);

        saasParkPropertyHouseRenovationApply.setRelativeConstructorId(saasParkPropertyHouseRenovationConstructorInfo.getId());

        saasParkPropertyHouseRenovationApply.setUpdateTime(new Date());
        //保存
        boolean result = saveOrUpdate(saasParkPropertyHouseRenovationApply);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean shenHe(ShenHeApplyDto req) {
        SaasParkPropertyHouseRenovationApply saasParkPropertyHouseRenovationApply = this.getById(req.getId());
        saasParkPropertyHouseRenovationApply.setApplyStatus(req.getApplyStatus());
        if (saasParkPropertyHouseRenovationApply == null) {
            throw new RuntimeException("请选择有效申请");
        }
        //0待审核,1通过,2不通过
        if (req.getApplyStatus() == 0) {
            saasParkPropertyHouseRenovationApply.setSure(0);
        } else {
            saasParkPropertyHouseRenovationApply.setSure(1);
        }
        saasParkPropertyHouseRenovationApply.setUpdateTime(new Date());
        saasParkPropertyHouseRenovationApply.setRemark(req.getRemark());
        boolean result = saveOrUpdate(saasParkPropertyHouseRenovationApply);
        return result;
    }


    private List<SaasParkPropertyHouseRenovationApplyVo> listVO(List<SaasParkPropertyHouseRenovationApply> listRecords) {
        return (List) listRecords.stream().map(entity -> {
            SaasParkPropertyHouseRenovationApplyVo saasParkPropertyHouseRenovationApplyVo = detail(entity.getId(), entity).getData();
            return saasParkPropertyHouseRenovationApplyVo;
        }).collect(Collectors.toList());

    }

    public R<SaasParkPropertyHouseRenovationApplyVo> detail(Long id, SaasParkPropertyHouseRenovationApply saasParkPropertyHouseRenovationApply) {
        SaasParkPropertyHouseRenovationApplyVo saasParkPropertyHouseRenovationApplyVo = new SaasParkPropertyHouseRenovationApplyVo();
        BeanUtils.copyProperties(saasParkPropertyHouseRenovationApply, saasParkPropertyHouseRenovationApplyVo);

        PmgBatParkInfo parkInfo = pmgBatParkInfoService.getById(saasParkPropertyHouseRenovationApply.getParkId());
        if (Func.isNotEmpty(parkInfo)) {
            saasParkPropertyHouseRenovationApplyVo.setParkName(parkInfo.getParkName());
        }

        PropertyBuildings propertyBuildings = propertyBuildingsMapper.selectById(saasParkPropertyHouseRenovationApply.getBuildingId());
        if (propertyBuildings != null) {
            saasParkPropertyHouseRenovationApplyVo.setBuildingName(propertyBuildings.getBuildingName());
        }

        PropertyBuildingUnit propertyBuildingUnit = propertyBuildingUnitMapper.selectById(saasParkPropertyHouseRenovationApply.getUnitId());
        if (propertyBuildingUnit != null) {
            saasParkPropertyHouseRenovationApplyVo.setUnitName(propertyBuildingUnit.getUnitName());
        }

        SaasParkPropertyHouseRenovationPersonInfo saasParkPropertyHouseRenovationPersonInfo = saasParkPropertyHouseRenovationPersonInfoMapper.selectById(saasParkPropertyHouseRenovationApply.getRelativePersonId());
        if (saasParkPropertyHouseRenovationPersonInfo != null) {
            saasParkPropertyHouseRenovationApplyVo.setSaasParkPropertyHouseRenovationPersonInfo(saasParkPropertyHouseRenovationPersonInfo);
        }
        SaasParkPropertyHouseRenovationConstructorInfo saasParkPropertyHouseRenovationConstructorInfo = saasParkPropertyHouseRenovationConstructorInfoMapper.selectById(saasParkPropertyHouseRenovationApply.getRelativeConstructorId());
        if (saasParkPropertyHouseRenovationConstructorInfo != null) {
            saasParkPropertyHouseRenovationApplyVo.setSaasParkPropertyHouseRenovationConstructorInfo(saasParkPropertyHouseRenovationConstructorInfo);
        }

        return R.data(saasParkPropertyHouseRenovationApplyVo);
    }


}
