package cn.ezhizao.framework.task;


import cn.ezhizao.common.utils.StringUtils;
import cn.ezhizao.framework.qrCode.QcContent;
import cn.ezhizao.framework.qrCode.QrCodeService;
import cn.ezhizao.project.business.carrier.domain.BizCarrier;
import cn.ezhizao.project.business.carrier.service.IBizCarrierService;
import cn.ezhizao.project.business.equipment.domain.BizEquipmentDetail;
import cn.ezhizao.project.business.equipment.service.IBizEquipmentDetailService;
import cn.ezhizao.project.business.packageResourceGroup.domain.BizPackageResourceGroup;
import cn.ezhizao.project.business.packageResourceGroup.service.IBizPackageResourceGroupService;
import cn.hutool.core.date.DateTime;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask {
    @Value("${file.upload.path}")
    private String uploadPath;
    @Resource
    ObjectMapper mapper;
    @Resource
    QrCodeService qrCodeService;

    @Resource
    IBizEquipmentDetailService bizEquipmentDetailService;

    @Resource
    IBizPackageResourceGroupService bizPackageResourceGroupService;

    @Resource
    IBizCarrierService bizCarrierService;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams() {
        System.out.println("执行无参方法");
    }

    public void resetCurrentCode() {
        List<BizPackageResourceGroup> groupList =bizPackageResourceGroupService.list();
        groupList.forEach(l -> {
            l.setCode("000000");
            l.setYear(DateTime.now().toString("yy"));
        });
        bizPackageResourceGroupService.updateBatchById(groupList);
    }


    /**
     * 新生成二维码方法，只生成箱号字符串
     */
    public <T> void GenerateQcCode(T t) throws JsonProcessingException {
        String className = t.getClass().getName();
        String content;
        String path;
        QcContent qcContent = new QcContent();
        switch (className) {
            case "cn.ezhizao.project.business.equipment.domain.BizEquipmentDetail":
                BizEquipmentDetail equipment = (BizEquipmentDetail) t;
                qcContent.setEquipmentDetailId(equipment.getId());
                qcContent.setEquipmentDetailCode(equipment.getEquipmentCode());
                qcContent.setType("equipment");
                content = mapper.writeValueAsString(qcContent);
                path = uploadPath + "qcCode/equipment/" + qcContent.getEquipmentDetailCode() + ".png";
                equipment.setQcCode("/profile/upload/qcCode/equipment/" + qcContent.getEquipmentDetailCode() + ".png");
                qrCodeService.createNewCodeToFile(content, path, qcContent.getEquipmentDetailCode());
                bizEquipmentDetailService.updateById(equipment);
                break;

            case "cn.ezhizao.project.business.carrier.domain.BizCarrier":

                BizCarrier carrier = (BizCarrier) t;
                // 定义二维码图片的存储路径
                String carrierCode = carrier.getCode(); // 假设您仍然需要使用carrier的code
                path = uploadPath + "qcCode/carrier/" + carrierCode + ".png";

                // 更新carrier对象的qcCode属性
                carrier.setQcCode("/profile/upload/qcCode/carrier/" + carrierCode + ".png");

                // 直接使用字符串生成二维码并保存到文件
                // 假设createNewCodeToFile方法可以接受一个字符串参数
                qrCodeService.createNewCodeToFile(carrierCode, path, carrierCode);


//                bizCarrierService.updateById(carrier);
//                BizCarrier carrier = (BizCarrier) t;
//                qcContent.setCarrierId(carrier.getId());
//                qcContent.setCarrierCode(carrier.getCode());
//                qcContent.setType("carrier");
//                content = mapper.writeValueAsString(qcContent);
//                path = uploadPath + "qcCode/carrier/" + qcContent.getCarrierCode() + ".png";
//                carrier.setQcCode("/profile/upload/qcCode/carrier/" + qcContent.getCarrierCode() + ".png");
//                qrCodeService.createNewCodeToFile(content, path, qcContent.getCarrierCode());
                bizCarrierService.updateById(carrier);
                break;


            default:
                break;
        }
    }
    /** 原始二维码生成方法，生成JSON格式
     * public <T> void  GenerateQcCode(T t) throws JsonProcessingException {
     *         String className = t.getClass().getName();
     *         String content;
     *         String path;
     *         QcContent qcContent = new QcContent();
     *         switch (className) {
     *             case "cn.ezhizao.project.business.equipment.domain.BizEquipmentDetail":
     *                 BizEquipmentDetail equipment = (BizEquipmentDetail) t;
     *                 qcContent.setEquipmentDetailId(equipment.getId());
     *                 qcContent.setEquipmentDetailCode(equipment.getEquipmentCode());
     *                 qcContent.setType("equipment");
     *                 content = mapper.writeValueAsString(qcContent);
     *                 path = uploadPath + "qcCode/equipment/" + qcContent.getEquipmentDetailCode() + ".png";
     *                 equipment.setQcCode("/profile/upload/qcCode/equipment/" + qcContent.getEquipmentDetailCode() + ".png");
     *                 qrCodeService.createNewCodeToFile(content, path,qcContent.getEquipmentDetailCode());
     *                 bizEquipmentDetailService.updateById(equipment);
     *                 break;
     *
     *             case "cn.ezhizao.project.business.carrier.domain.BizCarrier":
     *                 BizCarrier carrier = (BizCarrier) t;
     *                 qcContent.setCarrierId(carrier.getId());
     *                 qcContent.setCarrierCode(carrier.getCode());
     *                 qcContent.setType("carrier");
     *                 content = mapper.writeValueAsString(qcContent);
     *                 path = uploadPath + "qcCode/carrier/" + qcContent.getCarrierCode() + ".png";
     *                 carrier.setQcCode("/profile/upload/qcCode/carrier/" + qcContent.getCarrierCode() + ".png");
     *                 qrCodeService.createNewCodeToFile(content, path,qcContent.getCarrierCode());
     *                 bizCarrierService.updateById(carrier);
     *                 break;
     *
     *
     *             default:
     *                 break;
     *         }
     *     }
     * */
}
