package com.cool.store.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cool.store.dao.HomeTemplateDAO;

import com.cool.store.dao.HomeTemplateUserMappingDAO;
import com.cool.store.entity.HomeTemplateDO;

import com.cool.store.entity.HomeTemplateUserMappingDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.response.HomeTemplateResponse;

import com.cool.store.service.HomeTemplateService;
import com.cool.store.service.HomeTemplateUserMappingService;
import com.cool.store.utils.poi.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Author: WangShuo
 * @Date: 2025/04/08/10:44
 * @Version 1.0
 * @注释:
 */
@Service
public class HomeTemplateUserMappingServiceImpl implements HomeTemplateUserMappingService {

    @Resource
    private HomeTemplateUserMappingDAO homeTemplateUserMappingDAO;
    @Resource
    private HomeTemplateService homeTemplateService;
    @Resource
    private HomeTemplateDAO homeTemplateDAO;
    @Override
    public HomeTemplateResponse getHomeTemplateByUserId(String userId) {

        HomeTemplateUserMappingDO homeTemplateRoleMappingById = homeTemplateUserMappingDAO.getHomeTemplateRoleMappingByUserId(userId);
        if (homeTemplateRoleMappingById == null){
            //使用默认模板 id =1
            return homeTemplateService.getHomeTemplateById(1L);
        }else{
            return homeTemplateService.getHomeTemplateById(homeTemplateRoleMappingById.getTemplateId());
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertOrUpdateHomeTemplateUserMapping(HomeTemplateResponse homeTemplateResponse, String userId) {
        //查询是否有记录
        HomeTemplateUserMappingDO homeTemplateRoleMappingByUserId = homeTemplateUserMappingDAO.getHomeTemplateRoleMappingByUserId(userId);
        if (homeTemplateRoleMappingByUserId == null){
            //获取默认模板 id =1
            HomeTemplateDO homeTemplate = homeTemplateDAO.getHomeTemplateById(1L);
            HomeTemplateDO homeTemplateDO = new HomeTemplateDO();
            homeTemplateDO.setTemplateName(homeTemplate.getTemplateName());
            homeTemplateDO.setTemplateDescription(homeTemplate.getTemplateDescription());
            String jsonString = JSONObject.toJSONString(homeTemplateResponse.getMiniOpenSourceHomeTemplate());
            homeTemplateDO.setMiniOpenComponentsJson(jsonString);
            homeTemplateDO.setIsDefault(false);
            homeTemplateDO.setDeleted(0);
            homeTemplateDO.setCreateId(userId);
            homeTemplateDO.setCreateTime(new Date());
            Long tempId = homeTemplateDAO.insertSelective(homeTemplateDO);
            homeTemplateRoleMappingByUserId = new HomeTemplateUserMappingDO();
            homeTemplateRoleMappingByUserId.setTemplateId(tempId);
            homeTemplateRoleMappingByUserId.setUserId(userId);
            homeTemplateRoleMappingByUserId.setCreateId(userId);
            homeTemplateRoleMappingByUserId.setCreateTime(new Date());
          return   homeTemplateUserMappingDAO.insertSelective(homeTemplateRoleMappingByUserId);
        }else{
            HomeTemplateDO homeTemplateById = homeTemplateDAO.getHomeTemplateById(homeTemplateRoleMappingByUserId.getTemplateId());
            if (homeTemplateById == null){
                //如果模板不存在，则新建
                HomeTemplateDO homeTemplate = homeTemplateDAO.getHomeTemplateById(1L);
                HomeTemplateDO homeTemplateDO = new HomeTemplateDO();
                homeTemplateDO.setTemplateName(homeTemplate.getTemplateName());
                homeTemplateDO.setTemplateDescription(homeTemplate.getTemplateDescription());
                String jsonString = JSONObject.toJSONString(homeTemplateResponse.getMiniOpenSourceHomeTemplate());
                homeTemplateDO.setMiniOpenComponentsJson(jsonString);
                homeTemplateDO.setIsDefault(false);
                homeTemplateDO.setDeleted(0);
                homeTemplateDO.setCreateId(userId);
                homeTemplateDO.setCreateTime(new Date());
                Long tempId = homeTemplateDAO.insertSelective(homeTemplateDO);
                homeTemplateRoleMappingByUserId.setTemplateId(tempId);
                homeTemplateRoleMappingByUserId.setUpdateId(userId);
                homeTemplateRoleMappingByUserId.setUpdateTime(new Date());
            }else{
                homeTemplateById.setMiniOpenComponentsJson(JSONObject.toJSONString(homeTemplateResponse.getMiniOpenSourceHomeTemplate()));
                homeTemplateById.setUpdateId(userId);
                homeTemplateById.setUpdateTime(new Date());
                homeTemplateDAO.updateByPrimaryKeySelective(homeTemplateById);
            }
            return   homeTemplateUserMappingDAO.updateByPrimaryKeySelective(homeTemplateRoleMappingByUserId);
        }

    }


}
