package com.bjtdba.userCenter.service.impl;

import com.bjtdba.entity.userCenter.ProductShare;
import com.bjtdba.userCenter.dao.ProductShareDao;
import com.bjtdba.userCenter.service.UserShareProductService;
import com.bjtdba.utils.ResponseUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserShareProductServiceImpl implements UserShareProductService {

    @Autowired
    private ProductShareDao productShareDao;
    @Override
    public Object UserShareProductList(Integer pageNum, Integer pageSize, Integer uid, String start_time, String end_time) {
        Map<String, Object> data = new HashMap<>();

        PageHelper.startPage(pageNum,pageSize);
        List<ProductShare> productShareList = productShareDao.selectUserShareProductList(uid,start_time,end_time);

        //分享次数
        int sum = productShareList.size();
        if(sum!=0){
            //点击次数
            int clickNumSum = productShareDao.selectClickNum(uid,start_time,end_time);
            //购买次数
            int buyNumSum = productShareDao.selectBuyNum(uid,start_time,end_time);
            PageInfo<ProductShare> pageInfo = new PageInfo<ProductShare>(productShareList);
            DecimalFormat decimalFormat = new DecimalFormat("0.00");
            String ctr = "";
            String buy = "";
            if(clickNumSum != 0 ){
                ctr = decimalFormat.format((float) clickNumSum / (float) sum *100);
                buy = decimalFormat.format((float) buyNumSum / (float) clickNumSum *100);
            }else{
                ctr = "0.00";
                buy = "0.00";
            }
            data.put("ctr",ctr);
            data.put("buy",buy);
            data.put("pageInfo",pageInfo);
            return ResponseUtil.ok(data);
        }else{
            data.put("ctr","");
            data.put("buy","");
            data.put("pageInfo","");
            return ResponseUtil.ok(data);
        }
    }
}
