package com.org.hktshop.service.user.impl;

import com.org.hktshop.base.BaseService;
import com.org.hktshop.entity.order.HktOrder;
import com.org.hktshop.entity.user.HktUser;
import com.org.hktshop.entity.user.HktUserPoint;
import com.org.hktshop.service.order.HktOrderService;
import com.org.hktshop.service.user.HktUserPointService;
import com.org.hktshop.service.user.HktUserService;
import com.org.hktshop.utils.DateUtil;
import com.org.hktshop.utils.ExecSqlLib;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户积分记录
 * @author hotking03
 */
@Service
public class HktUserPointServiceImpl extends BaseService<HktUserPoint> implements HktUserPointService {
    @Autowired
    private HktUserService hktUserService;
    @Autowired
    private HktOrderService hktOrderService;
    @Override
    public void addUserPoint(HktUserPoint hktUserPoint) {
        String sql = ExecSqlLib.GetInsertSql(hktUserPoint);
        if(this.baseJdbcDao.execute(sql)){
            HktUser hktUser = new HktUser();
            hktUser.setId(hktUserPoint.getUserId());
            hktUser.setConsumePoint(hktUserPoint.getPoint());
            hktUserService.updateUserPoint(hktUser);
        }
    }

    @Override
    public List<HktUserPoint> queryList(HktUserPoint hktUserPoint) {
        String sql = ExecSqlLib.GetQuerySql(hktUserPoint);
        return this.baseJdbcDao.queryList(sql,hktUserPoint);
    }

    @Override
    public void addUserPoint(String orderSn) {
        List<String> sqls = new ArrayList<>();
        HktOrder hktOrder = hktOrderService.getHktOrderById(orderSn);
        HktUserPoint hktUserPoint = new HktUserPoint();
        hktUserPoint.setUserId(hktOrder.getUserId());
        hktUserPoint.setType(1);
        hktUserPoint.setStatus(1);
        hktUserPoint.setPoint(Integer.parseInt(new DecimalFormat("0").format(Double.parseDouble(hktOrder.getTotalAmount()))));
        hktUserPoint.setCreateTime(DateUtil.getDateTime());
        hktUserPoint.setOrderSn(orderSn);
        sqls.add(ExecSqlLib.GetInsertSql(hktUserPoint));
        HktUser hktUser = new HktUser();
        hktUser.setId(hktOrder.getUserId());
        hktUser.setConsumePoint(Integer.parseInt(new DecimalFormat("0").format(Double.parseDouble(hktOrder.getTotalAmount()))));
        hktUserService.updateUserPoint(hktUser);
        if(!"0".equals(hktOrder.getPointStatus())){
            HktUserPoint hp = new HktUserPoint();
            hp.setUserId(hktOrder.getUserId());
            hp.setType(2);
            hp.setStatus(3);
            hp.setPoint(1000);
            hp.setCreateTime(DateUtil.getDateTime());
            hp.setOrderSn(orderSn);
            sqls.add(ExecSqlLib.GetInsertSql(hp));
            HktUser hktUser1 = new HktUser();
            hktUser1.setId(hktOrder.getUserId());
            hktUser1.setConsumePoint(-1000);
            hktUserService.updateUserPoint(hktUser1);
        }
        this.baseJdbcDao.execute(sqls);
    }
}
