package com.cool.store.service.config.impl;

import com.cool.store.context.LoginUserInfo;
import com.cool.store.dao.config.ConfigItemCategoryDAO;
import com.cool.store.dao.config.ConfigItemDAO;
import com.cool.store.entity.config.ConfigItemCategoryDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.request.config.ConfigItemCategoryQueryRequest;
import com.cool.store.request.config.ConfigItemCategoryUpdateRequest;
import com.cool.store.service.config.ConfigItemCategoryService;
import com.cool.store.utils.StringUtil;
import com.cool.store.utils.UUIDUtils;
import com.cool.store.vo.config.ConfigItemCategoryVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ConfigItemCategoryServiceImpl implements ConfigItemCategoryService {

    private final ConfigItemCategoryDAO configItemCategoryDAO;
    private final ConfigItemDAO configItemDAO;

    @Override
    public Boolean add(ConfigItemCategoryUpdateRequest request, LoginUserInfo userInfo) {
        if (StringUtil.isEmpty(request.getCategoryCode())){
            request.setCategoryCode(UUIDUtils.get8UUID());
        }
        if (configItemDAO.existsItemCode(request.getCategoryCode(),request.getId())){
            throw new ServiceException(ErrorCodeEnum.CODE_EXIST);
        }
        ConfigItemCategoryDO entity = new ConfigItemCategoryDO();
        entity.setCategoryCode(request.getCategoryCode());
        entity.setCategoryName(request.getCategoryName());
        entity.setSortNo(request.getSortNo());
        entity.setImageUrl(request.getImageUrl());
        entity.setVideoUrl(request.getVideoUrl());
        entity.setStatus(request.getStatus());
        entity.setRemark(request.getRemark());
        entity.setCreatedTime(new Date());
        entity.setCreatedUserId(userInfo.getUserId());
        entity.setIsDeleted(0);
        return configItemCategoryDAO.insertSelective(entity);
    }

    @Override
    public Boolean update(ConfigItemCategoryUpdateRequest request,LoginUserInfo userInfo) {
        if (StringUtil.isNotEmpty(request.getCategoryCode())&&configItemDAO.existsItemCode(request.getCategoryCode(),request.getId())){
            throw new ServiceException(ErrorCodeEnum.CODE_EXIST);
        }
        ConfigItemCategoryDO entity = new ConfigItemCategoryDO();
        entity.setId(request.getId());
        entity.setCategoryName(request.getCategoryName());
        entity.setSortNo(request.getSortNo());
        entity.setImageUrl(request.getImageUrl());
        entity.setVideoUrl(request.getVideoUrl());
        entity.setStatus(request.getStatus());
        entity.setRemark(request.getRemark());
        entity.setUpdatedTime(new Date());
        entity.setUpdatedUserId(userInfo.getUserId());
        return configItemCategoryDAO.updateSelective(entity);
    }

    @Override
    public PageInfo<ConfigItemCategoryVO> queryPage(ConfigItemCategoryQueryRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<ConfigItemCategoryVO> list = configItemCategoryDAO.queryByCondition(request.getCategoryName(), request.getStatus());
        return new PageInfo<>(list);
    }

    @Override
    public Boolean deleteByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        if (configItemDAO.countByCategoryIds(ids) > 0) {
            return false;
        }
        return configItemCategoryDAO.softDeleteByIds(ids);
    }
}
