package shop.alien.store.service.impl;

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 lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.StoreStaffConfig;
import shop.alien.entity.store.excelVo.StoreInfoExpiredRecordsExcelVo;
import shop.alien.entity.store.excelVo.StoreStaffConfigExcelVo;
import shop.alien.entity.store.excelVo.util.ExcelGenerator;
import shop.alien.mapper.StoreStaffConfigMapper;
import shop.alien.store.service.StoreStaffConfigService;
import shop.alien.util.ali.AliOSSUtil;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author: fcw
 * @CreateTime: 2025-04-11
 * @Description: 员工管理
 */

@Service
@RequiredArgsConstructor
public class StoreStaffConfigServiceImpl implements StoreStaffConfigService {

    private final StoreStaffConfigMapper storeStaffConfigMapper;

    private final AliOSSUtil aliOSSUtil;

    @Value("${spring.web.resources.excel-path}")
    private String excelPath;
    @Value("${spring.web.resources.excel-clearing-receipt}")
    private String excelClearingReceipt;
    @Value("${spring.web.resources.excel-generate-path}")
    private String excelGeneratePath;
    @Value("${spring.web.resources.url}")
    private String fileUrl;

    @Override
    public IPage<StoreStaffConfig> getStaffConfigList(int page, int size, String status) {
        IPage<StoreStaffConfig> storePage = new Page<>(page, size);
        QueryWrapper<StoreStaffConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(null != status && !status.isEmpty(), "status", status);
        queryWrapper.orderByDesc("created_time");
        return storeStaffConfigMapper.selectPage(storePage, queryWrapper);
    }

    @Override
    public int addOrUpdateStaffConfig(StoreStaffConfig storeStaffConfig) {
        if (StringUtils.isEmpty(storeStaffConfig.getId().toString())) {
            Date nowDate = new Date(System.currentTimeMillis());
            storeStaffConfig.setCreatedTime(nowDate);
            return storeStaffConfigMapper.insert(storeStaffConfig);
        } else {
            return storeStaffConfigMapper.updateById(storeStaffConfig);
        }
    }

    @Override
    public Integer audit(Integer id, Integer status, String rejectionReason) {
        StoreStaffConfig storeStaffConfig = new StoreStaffConfig();
        storeStaffConfig.setId(id);
        storeStaffConfig.setStatus(String.valueOf(status));
        storeStaffConfig.setRejectionReason(rejectionReason);
        return storeStaffConfigMapper.updateById(storeStaffConfig);
    }

    @Override
    public StoreStaffConfig getStaffConfigDeatail(Integer id) {
        return storeStaffConfigMapper.selectById(id);
    }

    @Override
    public String staffConfigExport(String status) throws IOException {
        QueryWrapper<StoreStaffConfig> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(status), "status", status);
        wrapper.eq("delete_flag", 0);
        wrapper.orderByDesc("created_time");
        List<StoreStaffConfig> storeStaffConfig = storeStaffConfigMapper.selectList(wrapper);
        storeStaffConfig.forEach(item -> {
            if ("0".equals(item.getStatus())) {
                item.setStatus("待审核");
            } else if ("1".equals(item.getStatus())) {
                item.setStatus("审核通过");
            } else {
                item.setStatus("审核拒绝");
            }

        });
        List<StoreStaffConfigExcelVo> storeStaffConfigs = new ArrayList<>();
        int index = 1;
        for (StoreStaffConfig record : storeStaffConfig) {
            StoreStaffConfigExcelVo storeCouponStatusVo = new StoreStaffConfigExcelVo();
            BeanUtils.copyProperties(record, storeCouponStatusVo);
            storeCouponStatusVo.setId(index++); // 设置序号
            storeStaffConfigs.add(storeCouponStatusVo);
        }
        String fileName = UUID.randomUUID().toString().replace("-", "");
        String filePath = ExcelGenerator.generateExcel(excelPath + excelGeneratePath + fileName + ".xlsx", storeStaffConfigs, StoreStaffConfigExcelVo.class);
        return aliOSSUtil.uploadFile(new File(filePath), "excel/" + fileName + ".xlsx");
    }

}
