package com.ruoyi.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.demo.entity.Goods;
import com.ruoyi.demo.entity.bo.GoodsBo;
import com.ruoyi.demo.entity.bo.GoodsDeleteBo;
import com.ruoyi.demo.entity.bo.PlanBo;
import com.ruoyi.demo.entity.vo.GoodsDetailVo;
import com.ruoyi.demo.entity.vo.GoodsListVo;
import com.ruoyi.demo.mapper.GoodsMapper;
import com.ruoyi.demo.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public int addGoods(Goods goods) {
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        goodsQueryWrapper.eq("appkey",goods.getAppkey());
        goodsQueryWrapper.eq("goods_id",goods.getGoodsId());
        goodsQueryWrapper.eq("vendor_id",goods.getVendorId());
        goodsQueryWrapper.eq("deleted","0");
        Goods one = goodsMapper.selectOne(goodsQueryWrapper);
        if (one != null)
            return 2;
        goods.setCreateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        return goodsMapper.insert(goods);
    }

    @Override
    public int deleteGoods(GoodsDeleteBo bo) {
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        goodsQueryWrapper.eq("appkey",bo.getAppkey());
        goodsQueryWrapper.eq("goods_id",bo.getGoodsId());
        goodsQueryWrapper.eq("vendor_id",bo.getVendorId());
        return goodsMapper.delete(goodsQueryWrapper);
    }

    @Override
    public PageInfo<List<GoodsListVo>> getGoodsList(PlanBo bo) {
        PageHelper.startPage(bo.getPageNum(),bo.getPageSize(),true);
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        if (bo.getText() != null)
            queryWrapper.and(goodsQueryWrapper -> {
                goodsQueryWrapper.like("goods_id",bo.getText())
                    .or()
                    .like("goods_name",bo.getText())
                    .or()
                    .like("vendor_id",bo.getText())
                    .or()
                    .like("vendor_name",bo.getText())
                    .or()
                    .like("goods_classify",bo.getText());
            });
        if (bo.getStartTime() != null)
            queryWrapper.apply("create_time >= STR_TO_DATE({0},'%Y-%m-%d %H:%i:%s')",bo.getStartTime());
        if (bo.getEndTime() != null)
            queryWrapper.apply("create_time <= STR_TO_DATE({0},'%Y-%m-%d %H:%i:%s')",bo.getEndTime());
        queryWrapper.eq("deleted","0");
        List<GoodsListVo> vos = new ArrayList<>();
        for (Goods goods : goodsMapper.selectList(queryWrapper)) {
            vos.add(new GoodsListVo(goods));
        }
        return new PageInfo(vos);
    }

    @Override
    public GoodsDetailVo getGoodsDetail(GoodsBo bo) {
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        goodsQueryWrapper.eq("appkey",bo.getAppkey());
        goodsQueryWrapper.eq("goods_id",bo.getGoodsId());
        goodsQueryWrapper.eq("vendor_id",bo.getVendorId());
        goodsQueryWrapper.eq("deleted","0");
        Goods goods = goodsMapper.selectOne(goodsQueryWrapper);
        return new GoodsDetailVo(goods);
    }

    @Override
    public int editGoods(Goods goods) {
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        goodsQueryWrapper.eq("appkey",goods.getAppkey());
        goodsQueryWrapper.eq("goods_id",goods.getGoodsId());
        goodsQueryWrapper.eq("vendor_id",goods.getVendorId());
        goodsQueryWrapper.eq("deleted","0");
        return goodsMapper.update(goods,goodsQueryWrapper);
    }
}
