package com.uas.finance.erp.service.impl;

import com.uas.finance.erp.model.Apply;
import com.uas.finance.erp.service.FinancingApplyDownService;
import com.uas.finance.model.FinancingApply;
import com.uas.finance.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

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

/**
 * created by shicr on 2017/12/26
 **/
@Service
public class FinancingApplyDownServiceImpl implements FinancingApplyDownService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public ModelMap covert(List<FinancingApply> financingApplies) {
        ModelMap map = new ModelMap();
        List<Apply> financingApplyList = new ArrayList<Apply>();
        //id 这边先提取出来，保存成功返回直接更新状态，轮询中间不作处理
        StringBuffer idStr = new StringBuffer();
        int size = 0;
        if (!CollectionUtils.isEmpty(financingApplies)) {
            for (FinancingApply financingApply : financingApplies) {
                financingApplyList.add(new Apply(financingApply));
                if (idStr.length() > 0) {
                    idStr.append(",");
                }
                idStr.append(financingApply.getId());
                size = size + 1;
            }
        }
        map.put("apply", financingApplyList);
        //System.out.println(JSON.toJSON(financingApplyList));
        map.put("idStr", idStr);
        map.put("size", size);
        return map;
    }


    @Override
    public void updateDownStatus(String idStr) {
        String sql = "update financingapply set fa_erpstatus = 203 , fa_status = 203 where fa_id in (" + idStr + ")";
        jdbcTemplate.execute(sql);
    }

}
