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.ConfigItemDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.request.config.ConfigItemQueryRequest;
import com.cool.store.request.config.ConfigItemUpdateRequest;
import com.cool.store.service.config.ConfigItemService;
import com.cool.store.utils.StringUtil;
import com.cool.store.utils.UUIDUtils;
import com.cool.store.vo.config.ConfigItemVO;
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;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class ConfigItemServiceImpl implements ConfigItemService {

    private final ConfigItemDAO configItemDAO;
    private final ConfigItemCategoryDAO configItemCategoryDAO;

    @Override
    public Boolean add(ConfigItemUpdateRequest request, LoginUserInfo userInfo) {
        if (StringUtil.isEmpty(request.getItemCode())){
            request.setItemCode(UUIDUtils.get8UUID());
        }
        if (StringUtil.isNotEmpty(request.getItemCode())&&configItemDAO.existsItemCode(request.getItemCode(),request.getId())){
            throw new ServiceException(ErrorCodeEnum.CODE_EXIST);
        }
        ConfigItemDO entity = new ConfigItemDO();
        entity.setItemName(request.getItemName());
        entity.setItemCode(request.getItemCode());
        entity.setCategoryId(request.getCategoryId());
        entity.setSortNo(request.getSortNo());
        entity.setStatus(request.getStatus());
        entity.setRemark(request.getRemark());
        entity.setExpenseTypeCode(request.getExpenseTypeCode());
        entity.setMultipleChoice(request.getMultipleChoice());
        entity.setCreatedTime(new Date());
        entity.setCreatedUserId(userInfo.getUserId());
        entity.setIsDeleted(0);
        return configItemDAO.insertSelective(entity);
    }

    @Override
    public Boolean update(ConfigItemUpdateRequest request, LoginUserInfo userInfo) {
        if (request.getId()==null){
            throw new ServiceException(ErrorCodeEnum.ERROR_MESSAGE);
        }
        if (StringUtil.isNotEmpty(request.getItemCode())&&configItemDAO.existsItemCode(request.getItemCode(),request.getId())){
            throw new ServiceException(ErrorCodeEnum.CODE_EXIST);
        }
        ConfigItemDO entity = new ConfigItemDO();
        entity.setId(request.getId());
        entity.setItemName(request.getItemName());
        entity.setCategoryId(request.getCategoryId());
        entity.setSortNo(request.getSortNo());
        entity.setStatus(request.getStatus());
        entity.setRemark(request.getRemark());
        entity.setExpenseTypeCode(request.getExpenseTypeCode());
        entity.setMultipleChoice(request.getMultipleChoice());
        entity.setUpdatedTime(new Date());
        entity.setUpdatedUserId(userInfo.getUserId());
        return configItemDAO.updateSelective(entity);
    }

    @Override
    public PageInfo<ConfigItemVO> queryPage(ConfigItemQueryRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<ConfigItemVO> list = configItemDAO.queryByCondition(request.getItemName(), request.getItemCode(), request.getCategoryId(), request.getStatus());
        return new PageInfo<>(list);
    }

    @Override
    public List<ConfigItemVO> listByCategoryId(Long categoryId) {
        return configItemDAO.queryByCondition(null, null, categoryId,1);
    }

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