package com.hfkj.service.impl;


import com.hfkj.dao.CmsCategoryRoleMapper;
import com.hfkj.entity.CmsCategoryRoleExample;
import com.hfkj.service.CmsCategoryRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service(value = "CmsCategoryRoleService")
public class CmsCategoryRoleServiceImpl implements CmsCategoryRoleService {

    @Resource
    private CmsCategoryRoleMapper cmsCategoryRoleMapper;

    @Override
    @Transactional
    public int addCategoryRoles(Long categoryId, List<Integer> roleList) throws Exception {

        // 先删除该分类已存在的关联
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("categoryId", categoryId.toString());
        delCategoryRoles(paramsMap);

        return cmsCategoryRoleMapper.addCategoryRoles(categoryId, roleList);
    }

    @Override
    public int delCategoryRoles(Map<String, String> paramsMap) throws Exception {
        CmsCategoryRoleExample example = new CmsCategoryRoleExample();
        CmsCategoryRoleExample.Criteria criteria = example.createCriteria();

        criteria.andCategoryIdEqualTo(Long.valueOf(paramsMap.get("categoryId")));
        if (StringUtils.isNotBlank(paramsMap.get("roleType"))) {
            criteria.andRoleTypeEqualTo(Integer.valueOf(paramsMap.get("roleType")));
        }

        return cmsCategoryRoleMapper.deleteByExample(example);
    }

    @Override
    @Transactional
    public int delSurplusRolesOfCategories(List<Long> categoryIds, List<Integer> roleList) throws Exception {
        return cmsCategoryRoleMapper.delSurplusRolesOfCategories(categoryIds, roleList);
    }
}
