package com.fzm.mall.server.admin.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.exception.MyException;
import com.fzm.mall.server.admin.shop.entity.dto.NavigationDTO;
import com.fzm.mall.server.admin.shop.entity.dto.NavigationSaveDTO;
import com.fzm.mall.server.admin.shop.entity.dto.NavigationUpdateDTO;
import com.fzm.mall.server.admin.shop.mapper.ShopNavigationMapper;
import com.fzm.mall.server.admin.shop.model.Navigation;
import com.fzm.mall.server.admin.shop.service.INavigationGoodsService;
import com.fzm.mall.server.admin.shop.service.INavigationService;
import com.fzm.mall.server.admin.util.CopyUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 店铺中心-店铺导航管理 服务实现类
 * </p>
 *
 * @author wulixian
 * @since 2021-04-26
 */
@Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class NavigationServiceImpl extends ServiceImpl<ShopNavigationMapper, Navigation> implements INavigationService {

    private final ShopNavigationMapper shopNavigationMapper;
    private final INavigationGoodsService navigationGoodsService;

    @Override
    public void save(NavigationSaveDTO shopNavigationSaveDTO) {
        Navigation navigation = CopyUtil.to(shopNavigationSaveDTO, Navigation.class);
        navigation.init();
        navigation.saveNavigationGoods(navigationGoodsService);

        shopNavigationMapper.insert(navigation);
    }

    @Override
    public void update(NavigationUpdateDTO shopNavigationUpdateDTO) {
        Navigation navigation = CopyUtil.to(shopNavigationUpdateDTO, Navigation.class);

        UpdateWrapper<Navigation> wrapper = new UpdateWrapper<>();
        wrapper.eq("navigation_id", navigation.getNavigationId())
                .eq("shop_id", navigation.getShopId())
                .eq("merchant_id", navigation.getMerchantId())
                .eq("update_time", navigation.getUpdateTime());
        int update = shopNavigationMapper.update(navigation, wrapper);
        if (update == 0) {
            throw new MyException(MallResponseError.UPDATE_FAIL);
        }

        navigation.updateNavigationGoods(navigationGoodsService);
    }

    @Override
    public void delete(String navigationId, String merchantId) {
        Navigation navigation = new Navigation();
        navigation.setNavigationId(navigationId);
        navigation.setMerchantId(merchantId);
        navigation.setStatus(Navigation.SHOP_NAVIGATION_STATUS_DELETED);

        navigation.deleteNavigationGoods(navigationGoodsService);

        UpdateWrapper<Navigation> wrapper = new UpdateWrapper<>();
        wrapper.eq("navigation_id", navigation.getNavigationId())
                .eq("merchant_id", navigation.getMerchantId());
        int update = shopNavigationMapper.update(navigation, wrapper);
        if (update == 0) {
            throw new MyException(MallResponseError.UPDATE_FAIL);
        }
    }

    @Override
    public NavigationDTO read(String navigationId, String merchantId) {
        QueryWrapper<Navigation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("navigation_id", navigationId)
                .eq("merchant_id", merchantId);
        Navigation navigation = shopNavigationMapper.selectOne(queryWrapper);

        navigation.listGoods(navigationGoodsService);
        return CopyUtil.to(navigation, NavigationDTO.class);
    }

    @Override
    public List<NavigationDTO> listAll(String merchantId) {
        QueryWrapper<Navigation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("merchant_id", merchantId)
                .eq("status", Navigation.SHOP_NAVIGATION_STATUS_EFFECTIVE)
                .orderByAsc("display_order");
        List<Navigation> navigationList = shopNavigationMapper.selectList(queryWrapper);
        navigationList.forEach(navigation -> {
            navigation.listGoods(navigationGoodsService);
        });
        return CopyUtil.toList(navigationList, NavigationDTO.class);
    }
}
