package com.ag.rate.account.service;

import com.ag.base.BaseService;
import com.ag.entity.fee.AccountPoints;
import com.ag.rate.account.dao.AccountPointsDao;
import com.ag.util.UUIDUtil;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/15 0015.
 */
@Service
public class AccountPointsService extends BaseService {

    @Autowired
    private AccountPointsDao apDao;

    /**
     * ?????????
     */
    @Transactional
    public void save(HttpServletRequest request){
        String pointId = request.getParameter("POINT_CODE");//????
        String pointName = request.getParameter("POINT_NAME");
        String ids = request.getParameter("ids");//???
        /*AccountPoints delAP = new AccountPoints();
        delAP.setPointCode(pointId);
        delAP.setTenancyId(super.getTenancyId(request));
        apDao.delete(delAP);*/
        JSONArray ja = JSONArray.parseArray(ids);
        List<AccountPoints> lst = new ArrayList<>();
        for(Object obj : ja.toArray()){
            AccountPoints accountPoints = new AccountPoints();
            accountPoints.setId(UUIDUtil.getUUID());
            super.setSaveUser(request, accountPoints);
            accountPoints.setItemId(obj.toString());
            accountPoints.setPointCode(pointId);
            accountPoints.setPointName(pointName);
            lst.add(accountPoints);
        }
        apDao.save(lst);
    }

    @Transactional
    public void del(HttpServletRequest request){
        String ids = request.getParameter("ids");//???
        JSONArray array = JSONArray.parseArray(ids);
        List<AccountPoints> lst = new ArrayList<>();
        for(Object obj : array.toArray()){
            AccountPoints accountPoints = new AccountPoints();
            accountPoints.setId(obj.toString());
            lst.add(accountPoints);
        }
        apDao.delete(lst);
    }

}
