package cn.ysk.cashier.mybatis.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.ysk.cashier.dto.shop.TbShopAdDto;
import cn.ysk.cashier.mybatis.entity.TbShopAd;
import cn.ysk.cashier.mybatis.mapper.TbShopAdMapper;
import cn.ysk.cashier.mybatis.service.TbShopAdService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author GYJ
 */
@Service
@RequiredArgsConstructor
public class TbShopAdServiceImpl extends ServiceImpl<TbShopAdMapper, TbShopAd> implements TbShopAdService {
    @Override
    public ResponseEntity<Object> createTbShopAd(TbShopAdDto adDto) {
        if (adDto.getShopId() == null || adDto.getShopId() == 0) {
            return ResponseEntity.badRequest().body("店铺ID不能为空");
        }
        if (StrUtil.isBlank(adDto.getImgUrl())) {
            return ResponseEntity.badRequest().body("图片地址不能为空");
        }
        TbShopAd shopAd = adDto.convertToTbShopAd();
        shopAd.setCreateTime(new Date());
        shopAd.setUpdateTime(new Date());
        baseMapper.insert(shopAd);
        return ResponseEntity.ok().body("新增成功");
    }

    @Override
    public ResponseEntity<Object> updateTbShopAd(TbShopAdDto adDto) {
        if (adDto.getShopId() == null || adDto.getShopId() == 0) {
            return ResponseEntity.badRequest().body("店铺ID不能为空");
        }
        if (StrUtil.isBlank(adDto.getImgUrl())) {
            return ResponseEntity.badRequest().body("图片地址不能为空");
        }
        TbShopAd shopAd = baseMapper.selectById(adDto.getId());
        if (shopAd == null) {
            return ResponseEntity.badRequest().body("广告不存在");
        }

        if (StrUtil.isNotBlank(adDto.getImgUrl())) {
            shopAd.setImgUrl(adDto.getImgUrl());
        }
        if (StrUtil.isNotBlank(adDto.getLinkPath())) {
            shopAd.setLinkPath(adDto.getLinkPath());
        }
        if (adDto.getBorderRadius() != null) {
            shopAd.setBorderRadius(adDto.getBorderRadius());
        }
        if (StrUtil.isNotBlank(adDto.getShowPosition())) {
            shopAd.setShowPosition(adDto.getShowPosition());
        }
        if (StrUtil.isNotBlank(adDto.getFrequency())) {
            shopAd.setFrequency(adDto.getFrequency());
        }
        if (adDto.getStatus() != null) {
            shopAd.setStatus(adDto.getStatus());
        }
        if (adDto.getSort() != null) {
            shopAd.setSort(adDto.getSort());
        }
        shopAd.setUpdateTime(new Date());

        baseMapper.updateById(shopAd);

        return ResponseEntity.ok().body("修改成功");
    }

    @Override
    public ResponseEntity<Object> deleteTbShopAd(Integer adId) {
        TbShopAd shopAd = baseMapper.selectById(adId);
        if (shopAd == null) {
            return ResponseEntity.badRequest().body("广告不存在");
        }

        baseMapper.deleteById(adId);

        return ResponseEntity.ok().body("删除成功");
    }

    @Override
    public ResponseEntity<Object> getTbShopAd(Map<String, Object> params) {
        QueryWrapper<TbShopAd> wrapper = new QueryWrapper<>();

        if (StrUtil.isBlank((String) params.get("shopId"))) {
            return ResponseEntity.badRequest().body("店铺ID不能为空");
        }

        wrapper.eq("shop_id", params.get("shopId"));

        if (StrUtil.isNotBlank((String) params.get("status"))) {
            wrapper.eq("status", params.get("status"));
        }

        if (StrUtil.isNotBlank((String) params.get("showPosition"))) {
            wrapper.eq("show_position", params.get("showPosition"));
        }

        if (StrUtil.isNotBlank((String) params.get("frequency"))) {
            wrapper.eq("frequency", params.get("frequency"));
        }

        wrapper.orderByDesc("sort");

        return ResponseEntity.ok().body(baseMapper.selectList(wrapper));
    }
}
