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

import cn.hutool.core.collection.CollStreamUtil;
import com.cool.store.context.CurrentUserHolder;
import com.cool.store.dao.EnterpriseUserDAO;
import com.cool.store.dao.ShopInfoDAO;
import com.cool.store.dao.fees.FeeStandardConfigDAO;
import com.cool.store.dao.modify.ModifyRecordDAO;
import com.cool.store.entity.ShopInfoDO;
import com.cool.store.entity.fees.FeeStandardConfigDO;
import com.cool.store.enums.BusinessModuleEnum;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.request.fees.FeeStandardConfigAddRequest;
import com.cool.store.request.fees.FeeStandardConfigPageRequest;
import com.cool.store.service.dict.impl.DictService;
import com.cool.store.service.fees.FeeStandardConfigService;
import com.cool.store.utils.BeanUtil;
import com.cool.store.vo.fees.FeeStandardConfigVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 加盟费用标准收费额配置Service实现
 */
@Service
@RequiredArgsConstructor
public class FeeStandardConfigServiceImpl implements FeeStandardConfigService {

    private final FeeStandardConfigDAO feeStandardConfigDAO;
    private final ModifyRecordDAO modifyRecordDAO;
    private final DictService dictService;
    private final EnterpriseUserDAO enterpriseUserDAO;
    private final ShopInfoDAO shopInfoDAO;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(FeeStandardConfigAddRequest request) {
        verifyUnique(request.getJoinBrand(), request.getExpenseType(), null);

        FeeStandardConfigDO configDO = BeanUtil.toBean(request, FeeStandardConfigDO.class);
        configDO.setCreateUserId(CurrentUserHolder.getUserId());
        feeStandardConfigDAO.insertSelective(configDO);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(FeeStandardConfigAddRequest request) {
        verifyUnique(request.getJoinBrand(), request.getExpenseType(), request.getId());

        FeeStandardConfigDO updateDO = BeanUtil.toBean(request, FeeStandardConfigDO.class);
        feeStandardConfigDAO.updateByPrimaryKeySelective(updateDO);

        modifyRecordDAO.insertRecord(BusinessModuleEnum.FEE_STANDARD_CONFIG, request.getId(), CurrentUserHolder.getUserId());
        return true;
    }

    @Override
    public PageInfo<FeeStandardConfigVO> page(FeeStandardConfigPageRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<FeeStandardConfigDO> list = feeStandardConfigDAO.selectByCondition(request.getJoinBrand(), request.getExpenseType());
        PageInfo<FeeStandardConfigDO> page = new PageInfo<>(list);
        Set<String> userIds = CollStreamUtil.toSet(list, FeeStandardConfigDO::getCreateUserId);
        Map<String, String> userNameMap = enterpriseUserDAO.getUserNameMap(new ArrayList<>(userIds));
        PageInfo<FeeStandardConfigVO> newPage = BeanUtil.toPage(page, FeeStandardConfigVO.class);
        newPage.getList().forEach(v -> v.setCreateUserName(userNameMap.get(v.getCreateUserId())));
        dictService.fillDictField(newPage.getList());
        return newPage;
    }

    @Override
    public List<FeeStandardConfigVO> getByShopId(Long shopId) {
        ShopInfoDO shopInfo = shopInfoDAO.getShopInfo(shopId);
        if (Objects.isNull(shopInfo)) {
            throw new ServiceException(ErrorCodeEnum.SHOP_NOT_EXIST);
        }
        List<FeeStandardConfigDO> list = feeStandardConfigDAO.selectByCondition(Integer.valueOf(shopInfo.getFranchiseBrand()), null);
        return BeanUtil.toList(list, FeeStandardConfigVO.class);
    }

    private void verifyUnique(Integer joinBrand, String expenseType, Long excludeId) {
        if (feeStandardConfigDAO.existJoinBrandAndExpenseType(joinBrand, expenseType.trim(), excludeId)) {
            throw new ServiceException(ErrorCodeEnum.FEE_STANDARD_UNIQUE_KEY_EXISTS);
        }
    }
}


