package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.LifeUser;
import shop.alien.entity.store.StoreDynamicDiscount;
import shop.alien.entity.store.vo.StoreDynamicDiscountVo;
import shop.alien.mapper.StoreDynamicDiscountMapper;
import shop.alien.store.service.LifeUserService;
import shop.alien.store.service.StoreDynamicDiscountService;
import shop.alien.entity.store.excelVo.util.ExcelExporter;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 商户动态折扣 服务实现类
 *
 * @author ssk
 * @since 2025-03-18
 */
@Service
@RequiredArgsConstructor
public class StoreDynamicDiscountServiceImpl extends ServiceImpl<StoreDynamicDiscountMapper, StoreDynamicDiscount> implements StoreDynamicDiscountService {

    private final StoreDynamicDiscountMapper storeDynamicDiscountMapper;

    private final LifeUserService lifeUserService;

    List<StoreDynamicDiscountVo> excelList = new ArrayList<>();

    /**
     * 分页查询动态折扣
     *
     * @param pageNum  页数
     * @param pageSize 页容
     * @param status   审核状态, 0:待审核, 1:已通过, 2:已拒绝
     * @param date     创建时间
     * @return IPage<StoreDynamicDiscountVo>
     */
    @Override
    public IPage<StoreDynamicDiscountVo> getPage(Integer pageNum, Integer pageSize, Integer status, String date) {
        QueryWrapper<StoreDynamicDiscountVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(null != status, "a.status", status);
        if (StringUtils.isNotEmpty(date)) {
            queryWrapper.between("a.created_time", date + "_00:00:00", date + "_23:59:59");
        }
        queryWrapper.eq("a.delete_flag", 0)
                .orderByDesc("a.created_time");
        IPage<StoreDynamicDiscountVo> page = storeDynamicDiscountMapper.getPage(new Page<>(pageNum, pageSize), queryWrapper);
        page.getRecords().forEach(storeDynamicDiscountVo -> {
            if ("0".equals(storeDynamicDiscountVo.getActivityCrowd())) {
                LambdaQueryWrapper<LifeUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.in(LifeUser::getId, storeDynamicDiscountVo.getActivityCrowd());
                List<LifeUser> list = lifeUserService.list(lambdaQueryWrapper);
                storeDynamicDiscountVo.setActivityCrowdList(list);
            }
        });
        excelList = page.getRecords();
        return page;
    }

    /**
     * 导出Excel
     *
     * @return ResponseEntity
     */
    @Override
    public ResponseEntity<byte[]> exportToExcel() {
        try {
            String[] headers = {"活动编号", "活动名称", "创建时间", "活动时间", "状态"};
            byte[] excelData = ExcelExporter.exportToExcel(excelList, headers, "动态折扣列表");
            HttpHeaders responseHeaders = new HttpHeaders();
            responseHeaders.add("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode("动态折扣列表.xlsx") + "\";charset=utf-8");
            responseHeaders.set("Charset", "UTF-8");
            return new ResponseEntity<>(excelData, responseHeaders, HttpStatus.OK);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
