package com.gree.mall.manager.logic.exchange;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.ExcelData;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.exchange.GiftExchangeCodeBean;
import com.gree.mall.manager.commonmapper.CountMapper;
import com.gree.mall.manager.enums.ExchangeCodeTypeEnum;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.GiftExchangeCode;
import com.gree.mall.manager.plus.service.GiftExchangeCodeService;
import com.gree.mall.manager.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class ExchangeLogic {

    @Autowired
    CountMapper countMapper;
    @Autowired
    GiftExchangeCodeService giftExchangeCodeService;
    @Autowired
    CommonLogic commonLogic;

    /**
     * 兑换码统计列表
     * @param keyword
     * @param adminWebsitId
     * @param pageNo
     * @param pageSize
     * @param request
     * @return
     */
    public IPage<GiftExchangeCodeBean> queryExchangeCodeCountList(String keyword, String adminWebsitId, Integer pageNo, Integer pageSize, HttpServletRequest request){
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        IPage<GiftExchangeCodeBean> giftExchangeCodeBeanIPage = countMapper
                .queryExchangeCodeCountList(new Page(pageNo, pageSize), keyword, adminWebsitId, adminUser.getAdminCompanyIds());
        return giftExchangeCodeBeanIPage;
    }

    /**
     * 兑换码列表
     */
    public IPage<GiftExchangeCode> list(String id,String userId,String orderId,Integer status, Integer pageNo, Integer pageSize, HttpServletRequest request){
        IPage<GiftExchangeCode> page = giftExchangeCodeService.lambdaQuery()
                .eq(GiftExchangeCode::getType, ExchangeCodeTypeEnum.SUB.toString())
                .eq(StringUtils.isNotEmpty(id),GiftExchangeCode::getId,id)
                .eq(StringUtils.isNotEmpty(userId),GiftExchangeCode::getUserId, userId)
                .eq(StringUtils.isNotEmpty(orderId),GiftExchangeCode::getOrderId,orderId)
                .eq(status != null, GiftExchangeCode::getStatus, status)
                .page(new Page<>(pageNo, pageSize));
        return page;
    }


    /**
     * 兑换码统计导出
     */
    public ExcelData exportCountData(List<GiftExchangeCodeBean> list){
        String[] titles = new String[]{"昵称", "电话", "所属网点", "首次生成时间", "生成数量", "生成总金额", "使用数量"};
        List<List<Object>> rows = new ArrayList<>();
        for (GiftExchangeCodeBean bean : list) {
            List<Object> row = new ArrayList<>();
            row.add(bean.getNickName());
            row.add(bean.getUserPhone());
            row.add(bean.getWebsitName());
            if(bean.getBindTime() == null){
                row.add("");
            }else {
                row.add(DateUtils.formatDate(bean.getBindTime()));
            }
            row.add(bean.getNum());
            row.add(bean.getTotalAmount());
            row.add(bean.getUseNum());
            rows.add(row);
        }
        ExcelData excelData = new ExcelData();
        excelData.setTitles(Arrays.asList(titles));
        excelData.setRows(rows);
        return excelData;
    }


    /**
     * 兑换码导出
     */
    public ExcelData exportData(List<GiftExchangeCode> list){
        String[] titles = new String[]{"优惠码", "生成时间", "金额", "有效期", "状态", "使用时间", "订单编号", "使用人昵称"};
        List<List<Object>> rows = new ArrayList<>();
        for (GiftExchangeCode bean : list) {
            List<Object> row = new ArrayList<>();
            row.add(bean.getId());
            if(bean.getBindTime() == null){
                row.add("");
            }else {
                row.add(DateUtils.formatDate(bean.getBindTime()));
            }
            row.add(bean.getAmount());
            if(bean.getEndTime() == null){
                row.add("");
            }else {
                row.add(DateUtils.formatDate(bean.getEndTime()));
            }
            row.add(bean.getStatus() == 1?"已使用":"未使用");
            row.add(bean.getOrderId());
            row.add(bean.getUseNickName());
            rows.add(row);
        }
        ExcelData excelData = new ExcelData();
        excelData.setTitles(Arrays.asList(titles));
        excelData.setRows(rows);
        return excelData;
    }

}
