package com.yuchen.saas.park.service.service.property.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.property.SaasParkPropertyBillInformationDto;
import com.yuchen.saas.park.api.dto.property.SaasParkPropertyBillInformationDto;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyBillArrears;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyBillInformation;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyBillInformation;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyBillArrearsVo;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyBillInformationVo;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyBillInformationVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyBillInformationMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyBillInformationService;
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-09-26
 */
@Slf4j
@Service
public class SaasParkPropertyBillInformationServiceImpl extends BaseServiceImpl<SaasParkPropertyBillInformationMapper, SaasParkPropertyBillInformation> implements ISaasParkPropertyBillInformationService {


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Object saveOrUpdateByParam(SaasParkPropertyBillInformationDto saasParkPropertyBillInformationDto) {
        SaasParkPropertyBillInformation saasParkPropertyBillInformation = new SaasParkPropertyBillInformation();
        BeanUtil.copyProperties(saasParkPropertyBillInformationDto, saasParkPropertyBillInformation);
        //保存
        boolean result = saveOrUpdate(saasParkPropertyBillInformation);
        return result;
    }

    @Override
    public void export(SaasParkPropertyBillInformationDto saasParkPropertyBillInformationDto, HttpServletResponse response) {
        LambdaQueryWrapper<SaasParkPropertyBillInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasParkPropertyBillInformation::getParkId, saasParkPropertyBillInformationDto.getParkId())
                //关联的账单id
                .eq(Func.isNotEmpty(saasParkPropertyBillInformationDto.getRelativeReceivableManageId()), SaasParkPropertyBillInformation::getRelativeReceivableManageId, saasParkPropertyBillInformationDto.getRelativeReceivableManageId())
                //单号
                .eq(Func.isNotEmpty(saasParkPropertyBillInformationDto.getRoomId()), SaasParkPropertyBillInformation::getRoomId, saasParkPropertyBillInformationDto.getRoomId())
                .orderByDesc(SaasParkPropertyBillInformation::getCreateTime);
        List<SaasParkPropertyBillInformation> list = baseMapper.selectList(queryWrapper);
        List<SaasParkPropertyBillInformationVo> saasParkPropertyBillInformationVos =new ArrayList<>();
        if (Func.isNotEmpty(list)) {
            saasParkPropertyBillInformationVos = listVO(list);
        }
        log.info("需要导出的数量条数： {}", saasParkPropertyBillInformationVos.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(saasParkPropertyBillInformationVos); //最终处理好的数据结果集list
        } catch (Exception e) {
            log.error("账单表导出异常");
            e.printStackTrace();
        }
    }


    @Override
    public R<IPage<SaasParkPropertyBillInformationVo>> getList(SaasParkPropertyBillInformationDto saasParkPropertyBillInformationDto, Query query) {


        Page<SaasParkPropertyBillInformation> page = new Page<>(query.getCurrent(), query.getSize());
        //分页查询
        LambdaQueryWrapper<SaasParkPropertyBillInformation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Func.isNotEmpty(saasParkPropertyBillInformationDto.getParkId()), SaasParkPropertyBillInformation::getParkId, saasParkPropertyBillInformationDto.getParkId())
                //关联的账单id
                .eq(Func.isNotEmpty(saasParkPropertyBillInformationDto.getRelativeReceivableManageId()), SaasParkPropertyBillInformation::getRelativeReceivableManageId, saasParkPropertyBillInformationDto.getRelativeReceivableManageId())
                //单号
                .eq(Func.isNotEmpty(saasParkPropertyBillInformationDto.getRoomId()), SaasParkPropertyBillInformation::getRoomId, saasParkPropertyBillInformationDto.getRoomId())
                .orderByDesc(SaasParkPropertyBillInformation::getCreateTime);
        IPage<SaasParkPropertyBillInformation> pages = this.page(page, lambdaQueryWrapper);

        List<SaasParkPropertyBillInformation> saasParkPropertyBillInformationList = pages.getRecords();

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

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

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

    }

    public R<SaasParkPropertyBillInformationVo> detail(Long id, SaasParkPropertyBillInformation saasParkPropertyBillInformation) {
        SaasParkPropertyBillInformationVo saasParkPropertyBillInformationVo = new SaasParkPropertyBillInformationVo();
        BeanUtils.copyProperties(saasParkPropertyBillInformation, saasParkPropertyBillInformationVo);
        return R.data(saasParkPropertyBillInformationVo);
    }
















}
