package com.hfkj.service.impl;


import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.dao.CmsCategoryModuleMapper;
import com.hfkj.entity.CmsCategory;
import com.hfkj.entity.CmsCategoryModule;
import com.hfkj.entity.CmsCategoryModuleExample;
import com.hfkj.model.CmsCategoryModuleModel;
import com.hfkj.service.CmsCategoryModuleService;
import com.hfkj.service.CmsCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service(value = "cmsCategoryModuleService")
public class CmsCategoryModuleServiceImpl implements CmsCategoryModuleService {

    @Resource
    private CmsCategoryModuleMapper cmsCategoryModuleMapper;
    @Resource
    private CmsCategoryService cmsCategoryService;

    @Override
    @Transactional
    public int addCategoryModule(CmsCategoryModule cmsCategoryModule) throws Exception {

        // 验证模板是否已存在
        isCategoryModuleExists(cmsCategoryModule);

        // 获取分类信息
        CmsCategory category = cmsCategoryService.getCategoryById(cmsCategoryModule.getCategoryId());
        if (category == null || category.getStatus() == 0) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.CMS_CATEGORY_NOT_FOUND, "");
        }
        cmsCategoryModule.setCode(category.getCode());

        return cmsCategoryModuleMapper.insertSelective(cmsCategoryModule);
    }

    @Override
    public int updateCategoryModule(CmsCategoryModule cmsCategoryModule) throws Exception {
        CmsCategoryModule categoryModule = cmsCategoryModuleMapper.selectByPrimaryKey(cmsCategoryModule.getId());
        if (categoryModule == null || categoryModule.getStatus() == 0) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.CMS_CATEGORY_MODULE_NOT_FOUND, "");
        }

        // 验证模板是否已存在
        isCategoryModuleExists(cmsCategoryModule);

        // 获取分类信息
        CmsCategory category = cmsCategoryService.getCategoryById(cmsCategoryModule.getCategoryId());
        if (category == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.CMS_CATEGORY_NOT_FOUND, "");
        }
        cmsCategoryModule.setCode(category.getCode());

        return cmsCategoryModuleMapper.updateByPrimaryKey(cmsCategoryModule);
    }

    @Override
    public int delCategoryModule(Long id) throws Exception {
        CmsCategoryModule categoryModule = cmsCategoryModuleMapper.selectByPrimaryKey(id);
        if (categoryModule == null || categoryModule.getStatus() == 0) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.CMS_CATEGORY_MODULE_NOT_FOUND, "");
        }

        categoryModule.setStatus(0);
        return cmsCategoryModuleMapper.updateByPrimaryKey(categoryModule);
    }

    @Override
    public CmsCategoryModule getCategoryModuleById(Long id) throws Exception {
        return cmsCategoryModuleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CmsCategoryModuleModel> getListCategoryModule(Map<String, Object> paramsMap) throws Exception {

        return cmsCategoryModuleMapper.getListCategoryModule(paramsMap);
    }

    /**
     * 检查当前分类下是否存在相同名称的模板
     */
    private Boolean isCategoryModuleExists(CmsCategoryModule cmsCategoryModule) throws Exception{
        CmsCategoryModuleExample example = new CmsCategoryModuleExample();
        CmsCategoryModuleExample.Criteria criteria = example.createCriteria();

        criteria.andCategoryIdEqualTo(cmsCategoryModule.getCategoryId());
        criteria.andModuleNameEqualTo(cmsCategoryModule.getModuleName());
        criteria.andStatusNotEqualTo(0);

        List<CmsCategoryModule> moduleList = cmsCategoryModuleMapper.selectByExample(example);
        if (moduleList != null && moduleList.size() > 1) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.CMS_CATEGORY_MODULE_EXISTS, "");
        } else if (moduleList != null && moduleList.size() == 1 && !moduleList.get(0).getId().equals(cmsCategoryModule.getId())) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.CMS_CATEGORY_MODULE_EXISTS, "");
        }

        return false;
    }

}
