package com.kmall.api.service.mk;

import com.kmall.api.dao.mk.ApiMkStorePromStatMapper;
import com.kmall.api.entity.OrderGoodsVo;
import com.kmall.api.entity.OrderVo;
import com.kmall.api.entity.UserVo;
import com.kmall.api.entity.mk.MkStorePromOrderRealVo;
import com.kmall.api.entity.mk.MkStorePromStatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 门店推广统计Service实现类
 *
 * @author huangyq
 * @date 2019-04-26 15:52:20
 */
@Service
public class ApiMkStorePromStatService {
    @Autowired
    private ApiMkStorePromStatMapper mkStorePromStatMapper;
    @Autowired
    private ApiMkStorePromOrderRealService apiMkStorePromOrderRealService;

    public MkStorePromStatVo queryObject(Integer promStatId) {
        return mkStorePromStatMapper.queryObject(promStatId);
    }

    public MkStorePromStatVo queryObjectByPromId(Integer promId) {
        return mkStorePromStatMapper.queryObjectByPromId(promId);
    }

    public List<MkStorePromStatVo> queryList(Map<String, Object> map) {
        return mkStorePromStatMapper.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return mkStorePromStatMapper.queryTotal(map);
    }

    public int save(MkStorePromStatVo mkStorePromStat) {
        return mkStorePromStatMapper.save(mkStorePromStat);
    }

    public int update(MkStorePromStatVo mkStorePromStat) {
        return mkStorePromStatMapper.update(mkStorePromStat);
    }

    public int delete(Integer promStatId) {
        return mkStorePromStatMapper.delete(promStatId);
    }

    public int deleteBatch(Integer[]promStatIds) {
        return mkStorePromStatMapper.deleteBatch(promStatIds);
    }

    /**
     * 更新渠道追踪统计下单量数据
     * @param orderGoodsVo
     * @param orderInfo
     * @param loginUser
     * @param cartId
     */
    public void updateMkStorePromStat(OrderGoodsVo orderGoodsVo, OrderVo orderInfo, UserVo loginUser, Integer cartId){
        Integer goodsId = Integer.parseInt(String.valueOf(orderGoodsVo.getGoods_id()));
        MkStorePromStatVo statVo = mkStorePromStatMapper.queryStatDataByCartIdAndGoodsId(cartId, goodsId);
        if(statVo != null){
            updateStorePromStat(orderGoodsVo, orderInfo, loginUser, statVo);
        }
    }

    public void updateStorePromStat(OrderGoodsVo orderGoodsVo, OrderVo orderInfo, UserVo loginUser, MkStorePromStatVo statVo){
        MkStorePromOrderRealVo orderRealVo = new MkStorePromOrderRealVo();
        orderRealVo.setOrderId(Integer.parseInt(String.valueOf(orderInfo.getId())));
        orderRealVo.setOrderGoodsId(Integer.parseInt(String.valueOf(orderGoodsVo.getId())));
        orderRealVo.setModTime(new Date());
        orderRealVo.setPromOrderRealId(statVo.getPromOrderRealId());
        orderRealVo.setUserId(Integer.parseInt(String.valueOf(loginUser.getId())));
        apiMkStorePromOrderRealService.update(orderRealVo);

        MkStorePromStatVo mkStorePromStatVo = new MkStorePromStatVo();
        //获取推广订单关联表中根据推广id查询下单人数
        Integer submitNum = apiMkStorePromOrderRealService.querySubmitNumByPromId(statVo.getPromId());
        Integer submitOrdNum = apiMkStorePromOrderRealService.querySubmitOrderNumByPromId(statVo.getPromId());
        mkStorePromStatVo.setSubmitNum(submitNum == null ? 0 : submitNum);//下单人数
        mkStorePromStatVo.setSubmitOrderNum(submitOrdNum);//下单单数
        mkStorePromStatVo.setPromStatId(statVo.getPromStatId());
        mkStorePromStatMapper.update(mkStorePromStatVo);
    }
}
