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.PmgBatParkCompanyPersonDto;
import com.yuchen.saas.park.api.entity.park.PmgBatParkCompanyPerson;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyBillArrears;
import com.yuchen.saas.park.api.vo.park.PmgBatParkCompanyPersonVo;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyBillArrearsVo;
import com.yuchen.saas.park.service.mapper.PmgBatParkCompanyPersonMapper;
import com.yuchen.saas.park.service.service.IPmgBatParkCompanyPersonService;
import com.yuchen.saas.parking.api.vo.black.VehicleBlacklistVO;
import com.yuchen.saas.parking.charge.api.entity.alarm.PmgBatWarningInfo;
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 PmgBatParkCompanyPersonServiceImpl extends BaseServiceImpl<PmgBatParkCompanyPersonMapper, PmgBatParkCompanyPerson> implements IPmgBatParkCompanyPersonService {

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

        List<PmgBatParkCompanyPerson> list = baseMapper.selectList(lambdaQueryWrapper);
        List<PmgBatParkCompanyPersonVo> pmgBatParkCompanyPersonVos = new ArrayList<>();
        if (Func.isNotEmpty(list)) {
            pmgBatParkCompanyPersonVos = listVO(list);
        }
        log.info("需要导出的数量条数： {}", pmgBatParkCompanyPersonVos.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(pmgBatParkCompanyPersonVos); //最终处理好的数据结果集list
        } catch (Exception e) {
            log.error("企业成员表导出异常");
            e.printStackTrace();
        }

    }


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

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

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

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

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

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

    }

    public R<PmgBatParkCompanyPersonVo> detail(Long id, PmgBatParkCompanyPerson pmgBatParkCompanyPerson) {
        PmgBatParkCompanyPersonVo pmgBatParkCompanyPersonVo = new PmgBatParkCompanyPersonVo();
        BeanUtils.copyProperties(pmgBatParkCompanyPerson, pmgBatParkCompanyPersonVo);
        return R.data(pmgBatParkCompanyPersonVo);
    }














}
