package com.mortals.xhx.module.skin.service.impl;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.mortals.framework.common.Rest;
import com.mortals.xhx.common.code.SourceType;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.device.DeviceMsgReqPdu;
import com.mortals.xhx.common.pdu.skin.SkinBasePdu;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.feign.device.IDeviceMessageFeign;
import com.mortals.xhx.feign.site.ISiteFeign;
import com.mortals.xhx.feign.skin.ISkinBaseFeign;
import com.mortals.xhx.module.skin.model.SkinQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.skin.dao.SkinDao;
import com.mortals.xhx.module.skin.model.SkinEntity;
import com.mortals.xhx.module.skin.service.SkinService;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* SkinService
* 皮肤 service实现
*
* @author zxfei
* @date 2023-04-03
*/
@Service("skinService")
@Slf4j
public class SkinServiceImpl extends AbstractCRUDServiceImpl<SkinDao, SkinEntity, Long> implements SkinService {

    @Autowired
    private ISkinBaseFeign skinBaseFeign;

    @Override
    protected void updateBefore(SkinEntity entity, Context context) throws AppException {
        super.updateBefore(entity, context);

        if(YesNoEnum.YES.getValue()==entity.getUsed()){
            //修改非当前皮肤都未未使用
            SkinEntity skinEntity = new SkinEntity();
            skinEntity.setUsed(YesNoEnum.NO.getValue());

            SkinEntity condition = new SkinEntity();
            condition.setUsed(YesNoEnum.YES.getValue());
            this.updateBatch(skinEntity,condition,context);
        }
    }

    @Override
    public void syncSkin() {
        SkinBasePdu skinBasePdu = new SkinBasePdu();
        skinBasePdu.setProductName("样表机");
        skinBasePdu.setSize(-1);

        Rest<RespData<List<SkinBasePdu>>> skinBaseRest = skinBaseFeign.list(skinBasePdu);
        if (skinBaseRest.getCode() == YesNoEnum.YES.getValue()) {
            List<SkinBasePdu> skinBasePduList = skinBaseRest.getData().getData();
            log.info("样表机皮肤总数量：{}", skinBasePduList.size());
            if (!ObjectUtils.isEmpty(skinBasePduList)) {
                List<SkinEntity> newSkinList = skinBasePduList.stream().map(newSkin -> {
                    SkinEntity skinEntity = new SkinEntity();
                    skinEntity.initAttrValue();
                    //存相对地址
                    newSkin.setCssFilePath("/file"+ StrUtil.subAfter(newSkin.getCssFilePath(), "/file", false));
                    newSkin.setPreviewImagePath("/file"+StrUtil.subAfter(newSkin.getPreviewImagePath(), "/file", false));
                    BeanUtils.copyProperties(newSkin, skinEntity, BeanUtil.getNullPropertyNames(newSkin));
                    return skinEntity;
                }).collect(Collectors.toList());

                List<SkinEntity> oldSkinList = this.find(new SkinQuery());
                Map<Long, SkinEntity> oldSkinMap = oldSkinList.stream().collect(Collectors.toMap(x -> x.getId(), y -> y, (o, n) -> n));
                Map<Long, SkinEntity> newSkinMap = newSkinList.stream().collect(Collectors.toMap(x -> x.getId(), y -> y, (o, n) -> n));

                List<SkinEntity> updateSkinLsit = newSkinList.stream().map(item -> {
                    if (oldSkinMap.containsKey(item.getId())) {
                        item.setUsed(oldSkinMap.get(item.getId()).getUsed());
                        item.setUpdateTime(new Date());
                        return item;
                    }
                    return null;
                }).filter(f -> f != null).collect(Collectors.toList());

                List<SkinEntity> saveSkinList = newSkinList.stream().map(item -> {
                    if (!oldSkinMap.containsKey(item.getId())) {
                        item.setCreateUserId(1L);
                        item.setCreateUserName("系统管理员");
                        item.setCreateTime(new Date());
                        return item;
                    }
                    return null;
                }).filter(f -> f != null).collect(Collectors.toList());


                //做差集
                List<Long> delSkinList = oldSkinList.stream().map(item -> {
                    if (!newSkinMap.containsKey(item.getId())) {
                        return item.getId();
                    }
                    return null;
                }).filter(f -> f != null).collect(Collectors.toList());


                if (!ObjectUtils.isEmpty(updateSkinLsit)) {
                    log.info("皮肤更新，size:{}", updateSkinLsit.size());
                    this.update(updateSkinLsit);
                }

                if (!ObjectUtils.isEmpty(saveSkinList)) {
                    log.info("皮肤新增，size:{}", saveSkinList.size());
                    this.save(saveSkinList);
                }

                if (!ObjectUtils.isEmpty(delSkinList)) {
                    log.info("皮肤删除，size:{}", delSkinList.size());
                    this.remove(delSkinList, null);
                }

            }
        }
    }

    public static void main(String[] args) {
       // boolean b = StrUtil.subPre("http://192.168.0.98:11078/file/fileupload/1699947376437.png", "/file");
      //  System.out.println(b);

        String subAfter = StrUtil.subAfter("http://192.168.0.98:11078/file/fileupload/1699947376437.png", "/file", false);
        System.out.println(subAfter);
    }
}