package com.yami.shop.platform.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.PointsFailureRecord;
import com.yami.shop.bean.model.ShopCategory;
import com.yami.shop.bean.model.ShopCategoryLog;
import com.yami.shop.bean.vo.RecordExportVO;
import com.yami.shop.bean.vo.ShopCategoryLogExportVO;
import com.yami.shop.bean.vo.ShopCategoryLogVO;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.R;
import com.yami.shop.service.IShopCategoryLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 门店前台类目导入记录接口
 *
 * @author fbj
 * @since 2025-09-XX
 */
@Slf4j
@RestController
@RequestMapping("/platform/shopCategoryLog")
@Api(tags = "门店前台类目导入记录接口")
@AllArgsConstructor
public class ShopCategoryLogController {

    private final IShopCategoryLogService shopCategoryLogService;


    @GetMapping("/page")
    @ApiOperation(value = "分页查询门店前台类目导入记录", notes = "分页查询门店前台类目导入记录")
    public R<IPage<ShopCategoryLog>> page(PageParam<ShopCategoryLog> pageParam, ShopCategory shopCategory) {
        IPage<ShopCategoryLog> page = shopCategoryLogService.page(
                pageParam, new LambdaQueryWrapper<ShopCategoryLog>()
                        .like(shopCategory.getName() != null, ShopCategoryLog::getName, shopCategory.getName())
                        .eq(shopCategory.getShopId() != null, ShopCategoryLog::getShopId, shopCategory.getShopId())
                       );
        return R.SUCCESS(page);
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询门店前台类目导入记录", notes = "查询门店前台类目导入记录")
    public R<List<ShopCategoryLogVO>> list(ShopCategoryLog shopCategoryLog) {
        List<ShopCategoryLogVO> list = shopCategoryLogService.groupByCode(shopCategoryLog);
        return R.SUCCESS(list);
    }


    /**
     * 导出门店前台类目导入失败记录
     */
    @GetMapping("/export")
    @ApiOperation(value = "导出门店前台类目导入失败记录", notes = "导出门店前台类目导入失败记录")
    public void export(HttpServletResponse response, ShopCategoryLog shopCategoryLog) {
        List<ShopCategoryLog> list = shopCategoryLogService.list(new LambdaQueryWrapper<ShopCategoryLog>()
                .eq(ShopCategoryLog::getTaskCode, shopCategoryLog.getTaskCode())
                .eq(ShopCategoryLog::getIsFail, false));
        // 转换为导出VO
        List<ShopCategoryLogExportVO> exportData = new ArrayList<>();
        for (ShopCategoryLog record : list) {
            ShopCategoryLogExportVO exportVO = new ShopCategoryLogExportVO();

            exportVO.setHbShopId(record.getHbShopId());
            exportVO.setReason(record.getReason());
            exportVO.setName(record.getName());
            exportVO.setSubName(record.getSubName());
            exportVO.setSubSubName(record.getSubSubName());
            exportData.add(exportVO);

        }
        try {
            // 设置响应头
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("积分导入失败数据导出", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 写入Excel
            EasyExcel.write(response.getOutputStream(), ShopCategoryLogExportVO.class)
                    .sheet("积分导入失败数据")
                    .doWrite(exportData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
