package com.sqx.modules.goods.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.goods.dao.GoodsRuleMapper;
import com.sqx.modules.goods.entity.Goods;
import com.sqx.modules.goods.entity.GoodsAttr;
import com.sqx.modules.goods.entity.GoodsRule;
import com.sqx.modules.goods.entity.GoodsRuleValue;
import com.sqx.modules.goods.service.GoodsAttrService;
import com.sqx.modules.goods.service.GoodsRuleService;
import com.sqx.modules.goods.service.GoodsRuleValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service
public class GoodsRuleServiceImpl extends ServiceImpl<GoodsRuleMapper, GoodsRule> implements GoodsRuleService {

    @Autowired
    private GoodsRuleMapper goodsRuleMapper;
    @Autowired
    private GoodsRuleValueService goodsRuleValueService;
    @Autowired
    private GoodsAttrService goodsAttrService;



    @Override
    public Result findAll(Integer page, Integer limit, Long shopId, String shopName) {
       // List<GoodsRule> goodsRules = baseMapper.selectList(new QueryWrapper<GoodsRule>().eq(shopId!=null, "shop_id", shopId));
        if(page==null || limit==null){
            List<GoodsRule> goodsRules = baseMapper.selectList(new QueryWrapper<GoodsRule>().eq(shopId!=null, "shop_id", shopId));
            for (GoodsRule r : goodsRules) {
                r.setRuleValue(goodsRuleValueService.selectGoodsRuleValue(r.getId()));
            }
            return Result.success().put("data",goodsRules);
        }else{
            Page<GoodsRule> pages=new Page<>(page,limit);
            IPage<GoodsRule> goodsRules = goodsRuleMapper.selectRuleByShopId(pages,shopId, shopName);
            List<GoodsRule> records = goodsRules.getRecords();
            for (GoodsRule r : records) {
                r.setRuleValue(goodsRuleValueService.selectGoodsRuleValue(r.getId()));
            }
            return Result.success().put("data",goodsRules);
        }

    }

    @Override
    public Result info() {
        List<GoodsRule> all = baseMapper.selectList(null);
        for (GoodsRule r : all) {
            r.setRuleValue(goodsRuleValueService.selectGoodsRuleValue(r.getId()));
        }
        return Result.success().put("data",all);
    }

    @Transactional
    @Override
    public Result saveBody(GoodsRule goodsRule) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        goodsRule.setCreateTime(sdf.format(new Date())); //创建时间
        baseMapper.insertGoodsRule(goodsRule);
        List<GoodsRuleValue> list = goodsRule.getRuleValue();
        for (GoodsRuleValue goodsRuleValue : list) {
            goodsRuleValue.setRuleId(goodsRule.getId());
            goodsRuleValueService.save(goodsRuleValue);
        }
        return Result.success();
    }

    @Transactional
    @Override
    public Result updateBody(GoodsRule goodsRule) {
        goodsRuleValueService.deleteGoodsRuleValue(goodsRule.getId());
        //放入新值
        List<GoodsRuleValue> list = goodsRule.getRuleValue();
        for (GoodsRuleValue goodsRuleValue : list) {
            goodsRuleValue.setRuleId(goodsRule.getId());
            goodsRuleValueService.save(goodsRuleValue);
        }
        baseMapper.updateById(goodsRule);
        return Result.success();
    }

    @Override
    public Result findOne(Long id) {
        GoodsRule s = baseMapper.selectById(id);
        s.setRuleValue(goodsRuleValueService.selectGoodsRuleValue(id));
        return Result.success().put("data",s);
    }

    @Override
    public Result delete(Long id) {
        int ruleCount = goodsAttrService.count(new QueryWrapper<GoodsAttr>().eq("rule_id", id));
        if(ruleCount>0){
            return Result.error("当前规格有商品正在使用，请移除后删除！");
        }
        baseMapper.deleteById(id);
        return Result.success();
    }


}