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.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.home_page.dao.AdminMapper;
import com.fzm.mall.server.admin.shop.entity.dto.ShopDTO;
import com.fzm.mall.server.admin.shop.entity.dto.ShopSaveDTO;
import com.fzm.mall.server.admin.shop.entity.dto.ShopUpdateDTO;
import com.fzm.mall.server.admin.shop.mapper.ShopMapper;
import com.fzm.mall.server.admin.shop.model.Shop;
import com.fzm.mall.server.admin.shop.service.IShopService;
import com.fzm.mall.server.admin.util.CopyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 店铺中心-店铺 服务实现类
 * </p>
 *
 * @author wulixian
 * @since 2021-03-16
 */
@Service
@Transactional
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements IShopService {

    @Resource
    private ShopMapper shopMapper;

    @Resource
    private AdminMapper adminMapper;

    @Override
    public void save(ShopSaveDTO shopSaveDTO) {
        Shop shop = CopyUtil.to(shopSaveDTO, Shop.class);
        shop.init();
        shop.updateShopCustomer(adminMapper);

        shopMapper.insert(shop);
    }

    @Override
    public void update(ShopUpdateDTO shopUpdateDTO) {
        Shop shop = CopyUtil.to(shopUpdateDTO, Shop.class);
        /**
         * 店铺名称若有改变，同步修改客服名称
         */
        Shop shopData = shopMapper.selectOne(new QueryWrapper<Shop>().eq("shop_id", shop.getShopId())
                .eq("merchant_id", shop.getMerchantId()));
        if(!shopData.getShopName().equals(shop.getShopName())){
            shop.updateShopCustomer(adminMapper);
        }

        UpdateWrapper<Shop> wrapper = new UpdateWrapper<>();
        wrapper.eq("shop_id", shop.getShopId())
                .eq("merchant_id", shop.getMerchantId())
                .eq("update_time", shop.getUpdateTime());
        int update = shopMapper.update(shop, wrapper);
        if (update == 0) {
            throw new MyException(MallResponseError.UPDATE_FAIL);
        }

    }

    @Override
    public void delete(String shopId, String merchantId) {
        UpdateWrapper<Shop> wrapper = new UpdateWrapper<>();
        wrapper.eq("shop_id", shopId)
                .eq("merchant_id", merchantId)
                .set("status", Shop.SHOP_STATUS_INVALID);
        shopMapper.update(null, wrapper);
    }

    @Override
    public ShopDTO read(String shopId, String merchantId) {
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_id", shopId).eq("merchant_id", merchantId);
        Shop shop = shopMapper.selectOne(queryWrapper);
        return CopyUtil.to(shop, ShopDTO.class);
    }

    @Override
    public IPage<ShopDTO> page(Page<ShopDTO> page, String merchantId) {
        IPage<ShopDTO> result = shopMapper.page(page, merchantId);
        return result;
    }

    @Override
    public ShopDTO readByMerchantId(String merchantId) {
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("merchant_id", merchantId);
        Shop shop = shopMapper.selectOne(queryWrapper);
        return CopyUtil.to(shop, ShopDTO.class);
    }
}
