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

import com.alibaba.excel.EasyExcel;
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.dto.park.PmgBatParkCompanyRentDto;
import com.yuchen.saas.park.api.entity.park.PmgBatParkCompanyRent;
import com.yuchen.saas.park.api.vo.park.PmgBatParkCompanyRentVo;
import com.yuchen.saas.park.service.mapper.PmgBatParkCompanyRentMapper;
import com.yuchen.saas.park.service.service.IPmgBatParkCompanyRentService;
import com.yuchen.saas.parking.api.vo.black.VehicleBlacklistVO;
import lombok.extern.slf4j.Slf4j;
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.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 企业租客表 服务实现类
 * </p>
 *
 * @author baichaojie
 * @since 2024-11-05
 */
@Slf4j
@Service
public class PmgBatParkCompanyRentServiceImpl extends BaseServiceImpl<PmgBatParkCompanyRentMapper, PmgBatParkCompanyRent> implements IPmgBatParkCompanyRentService {


    /**
     * 人员中心+-企业租客-企业租客 导出
     * @param pmgBatParkCompanyRentDto
     * @param response
     */
    @Override
    public void export(PmgBatParkCompanyRentDto pmgBatParkCompanyRentDto, HttpServletResponse response) {
        LambdaQueryWrapper<PmgBatParkCompanyRent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //联系人
        lambdaQueryWrapper.like(Func.isNotEmpty(pmgBatParkCompanyRentDto.getContactPerson()), PmgBatParkCompanyRent::getContactPerson, pmgBatParkCompanyRentDto.getContactPerson());
        //项目id
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getParkId()), PmgBatParkCompanyRent::getParkId, pmgBatParkCompanyRentDto.getParkId());
        //单元id
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getUnitId()), PmgBatParkCompanyRent::getUnitId, pmgBatParkCompanyRentDto.getUnitId());
        //楼栋id
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getBuildingId()), PmgBatParkCompanyRent::getBuildingId, pmgBatParkCompanyRentDto.getBuildingId());
        //房屋id
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getRoomId()), PmgBatParkCompanyRent::getRoomId, pmgBatParkCompanyRentDto.getRoomId());
        //机构类型
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getCompanyType()), PmgBatParkCompanyRent::getCompanyType, pmgBatParkCompanyRentDto.getCompanyType());
        //机构名
        lambdaQueryWrapper.like(Func.isNotEmpty(pmgBatParkCompanyRentDto.getCompanyName()), PmgBatParkCompanyRent::getCompanyName, pmgBatParkCompanyRentDto.getCompanyName());
        //证件号码
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getIdNo()), PmgBatParkCompanyRent::getIdNo, pmgBatParkCompanyRentDto.getIdNo());
        //证件类型
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getIdType()), PmgBatParkCompanyRent::getIdType, pmgBatParkCompanyRentDto.getIdType());
        //联系方式
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getContactTel()), PmgBatParkCompanyRent::getContactTel, pmgBatParkCompanyRentDto.getContactTel());
        //认证状态
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getAuthenticationStatus()), PmgBatParkCompanyRent::getAuthenticationStatus, pmgBatParkCompanyRentDto.getAuthenticationStatus());
        //开始时间
        lambdaQueryWrapper.ge(Func.isNotEmpty(pmgBatParkCompanyRentDto.getStartTime()), PmgBatParkCompanyRent::getCreateTime,pmgBatParkCompanyRentDto.getStartTime());
        //结束时间
        lambdaQueryWrapper.le(Func.isNotEmpty(pmgBatParkCompanyRentDto.getEndTime()),PmgBatParkCompanyRent::getCreateTime,pmgBatParkCompanyRentDto.getEndTime());
        //证件号码
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getIdNo()), PmgBatParkCompanyRent::getIdNo, pmgBatParkCompanyRentDto.getIdNo())
                .orderByDesc(PmgBatParkCompanyRent::getCreateTime);

        List<PmgBatParkCompanyRent> list = baseMapper.selectList(lambdaQueryWrapper);
        List<PmgBatParkCompanyRentVo> pmgBatParkCompanyRentVos = new ArrayList<>();
        if (Func.isNotEmpty(list)) {
            pmgBatParkCompanyRentVos = listVO(list);
        }
        log.info("需要导出的数量条数： {}", pmgBatParkCompanyRentVos.size());
        try {
            String fileName = URLEncoder.encode("企业租客表", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), VehicleBlacklistVO.class)
                    .sheet("企业租客表")
                    .doWrite(pmgBatParkCompanyRentVos); //最终处理好的数据结果集list
        } catch (Exception e) {
            log.error("企业租客表导出异常");
            e.printStackTrace();
        }

    }


    /**
     * 人员中心+-企业租客-企业租客  新增或修改
     * @param pmgBatParkCompanyRentDto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveOrUpdateByParam(PmgBatParkCompanyRentDto pmgBatParkCompanyRentDto) {
        PmgBatParkCompanyRent pmgBatParkCompanyRent = new PmgBatParkCompanyRent();
        BeanUtil.copyProperties(pmgBatParkCompanyRentDto, pmgBatParkCompanyRent);
        //保存
        boolean result = saveOrUpdate(pmgBatParkCompanyRent);
        return result;
    }

    /**
     * 人员中心+-企业租客-企业租客  列表查询
     * @param pmgBatParkCompanyRentDto
     * @param query
     * @return
     */
    @Override
    public R<IPage<PmgBatParkCompanyRentVo>> getList(PmgBatParkCompanyRentDto pmgBatParkCompanyRentDto, Query query) {

        Page<PmgBatParkCompanyRent> page = new Page<>(query.getCurrent(), query.getSize());
        //分页查询
        LambdaQueryWrapper<PmgBatParkCompanyRent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //联系人
        lambdaQueryWrapper.like(Func.isNotEmpty(pmgBatParkCompanyRentDto.getContactPerson()), PmgBatParkCompanyRent::getContactPerson, pmgBatParkCompanyRentDto.getContactPerson());
        //项目id
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getParkId()), PmgBatParkCompanyRent::getParkId, pmgBatParkCompanyRentDto.getParkId());
        //单元id
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getUnitId()), PmgBatParkCompanyRent::getUnitId, pmgBatParkCompanyRentDto.getUnitId());
        //楼栋id
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getBuildingId()), PmgBatParkCompanyRent::getBuildingId, pmgBatParkCompanyRentDto.getBuildingId());
        //房屋id
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getRoomId()), PmgBatParkCompanyRent::getRoomId, pmgBatParkCompanyRentDto.getRoomId());
        //机构类型
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getCompanyType()), PmgBatParkCompanyRent::getCompanyType, pmgBatParkCompanyRentDto.getCompanyType());
        //机构名
        lambdaQueryWrapper.like(Func.isNotEmpty(pmgBatParkCompanyRentDto.getCompanyName()), PmgBatParkCompanyRent::getCompanyName, pmgBatParkCompanyRentDto.getCompanyName());
        //证件号码
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getIdNo()), PmgBatParkCompanyRent::getIdNo, pmgBatParkCompanyRentDto.getIdNo());
        //证件类型
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getIdType()), PmgBatParkCompanyRent::getIdType, pmgBatParkCompanyRentDto.getIdType());
        //联系方式
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getContactTel()), PmgBatParkCompanyRent::getContactTel, pmgBatParkCompanyRentDto.getContactTel());
        //认证状态
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getAuthenticationStatus()), PmgBatParkCompanyRent::getAuthenticationStatus, pmgBatParkCompanyRentDto.getAuthenticationStatus());
        //开始时间
        lambdaQueryWrapper.ge(Func.isNotEmpty(pmgBatParkCompanyRentDto.getStartTime()), PmgBatParkCompanyRent::getCreateTime,pmgBatParkCompanyRentDto.getStartTime());
        //结束时间
        lambdaQueryWrapper.le(Func.isNotEmpty(pmgBatParkCompanyRentDto.getEndTime()),PmgBatParkCompanyRent::getCreateTime,pmgBatParkCompanyRentDto.getEndTime());
        //证件号码
        lambdaQueryWrapper.eq(Func.isNotEmpty(pmgBatParkCompanyRentDto.getIdNo()), PmgBatParkCompanyRent::getIdNo, pmgBatParkCompanyRentDto.getIdNo())
                .orderByDesc(PmgBatParkCompanyRent::getCreateTime);
        IPage<PmgBatParkCompanyRent> pages = this.page(page, lambdaQueryWrapper);
        List<PmgBatParkCompanyRent> pmgBatParkCompanyRentList = pages.getRecords();

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

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

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

    }

    public R<PmgBatParkCompanyRentVo> detail(Long id, PmgBatParkCompanyRent pmgBatParkCompanyRent) {
        PmgBatParkCompanyRentVo pmgBatParkCompanyRentVo = new PmgBatParkCompanyRentVo();
        BeanUtils.copyProperties(pmgBatParkCompanyRent, pmgBatParkCompanyRentVo);
        return R.data(pmgBatParkCompanyRentVo);
    }






















}
