package com.ycwl.basic.service.pc.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycwl.basic.biz.PriceBiz;
import com.ycwl.basic.mapper.PriceConfigMapper;
import com.ycwl.basic.model.pc.price.entity.PriceConfigEntity;
import com.ycwl.basic.model.pc.price.req.PriceConfigListReq;
import com.ycwl.basic.model.pc.price.resp.GoodsListRespVO;
import com.ycwl.basic.model.pc.price.resp.PriceConfigRespVO;
import com.ycwl.basic.model.pc.template.resp.TemplateRespVO;
import com.ycwl.basic.repository.TemplateRepository;
import com.ycwl.basic.service.pc.PriceConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PriceConfigServiceImpl extends ServiceImpl<PriceConfigMapper, PriceConfigEntity> implements PriceConfigService {

    @Autowired
    private TemplateRepository templateRepository;
    @Autowired
    private PriceBiz priceBiz;

    @Override
    public List<PriceConfigRespVO> listByCondition(PriceConfigListReq req) {
        return baseMapper.listByCondition(req);
    }

    @Override
    public PriceConfigRespVO findById(Integer id) {
        return baseMapper.getById(id);
    }

    @Override
    public List<GoodsListRespVO> listGoodsByScenic(Long scenicId) {
        return priceBiz.listGoodsByScenic(scenicId);
    }

    @Override
    public void fillGoodsName(List<PriceConfigRespVO> result) {
        for (PriceConfigRespVO item : result) {
            fillGoodsName(item);
        }
    }

    @Override
    public void fillGoodsName(PriceConfigRespVO item) {
        if (Integer.valueOf(-1).equals(item.getType())) {
            item.setGoodsNames("景区内所有售卖商品");
        } else if (Integer.valueOf(3).equals(item.getType())) {
            item.setGoodsNames("打印照片");
        } else if (StringUtils.isNotBlank(item.getGoodsIds())) {
            List<String> goodsNames = new ArrayList<>();
            for (String s : item.getGoodsIds().split(",")) {
                if (StringUtils.equals(s, "1")) {
                    goodsNames.add("录像集");
                } else if (StringUtils.equals(s, "2")) {
                    goodsNames.add("照片集");
                } else {
                    if (StringUtils.isNumeric(s)) {
                        TemplateRespVO template = templateRepository.getTemplate(Long.valueOf(s));
                        if (template != null) {
                            goodsNames.add(template.getName());
                        } else {
                            goodsNames.add("?未知商品【"+s+"】?");
                        }
                    } else {
                        goodsNames.add("!未知商品【"+s+"】!");
                    }
                }
            }
            item.setGoodsNames(StringUtils.join(goodsNames, ","));
        }
    }

    @Override
    public void updateStatus(Integer id) {
        baseMapper.updateStatus(id);
    }
}