package com.uas.finance.service.Impl;

import com.uas.finance.model.CustomerInfo;import com.uas.finance.service.CustomerInfoService;import com.uas.finance.dao.CustomerInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户资料表接口实现层
 * created by shicr on 2017/12/18
 **/
@Service
public class CustomerInfoServiceImpl implements CustomerInfoService{

    @Autowired
    private CustomerInfoDao dao;

    @Autowired
    JdbcTemplate jdbcTemplate;

    /**
     * 保存客户资料表
     * @param info
     * @return
     */
    @Override
    public CustomerInfo save(CustomerInfo info) {
        return dao.save(info);
    }

    /**
     * 更新erp传来的客户资料
     *
     * @param customerInfo
     * @return
     */
    @Override
    public List<Map<String, Long>> update(CustomerInfo customerInfo) {
        List<Map<String, Long>> list = new ArrayList<Map<String, Long>>();
        Map<String,Long> map = new HashMap<String,Long>();
        CustomerInfo customerInfo1 = dao.save(customerInfo);
        map.put("id",customerInfo1.getId());
        map.put("cu_id",customerInfo1.getErpId());
        list.add(map);
        return list;
    }

    /**
     * 找到某企业的待下载数据
     * @param enuu
     * @param status
     * @return
     */
    @Override
    public List<CustomerInfo> findByEnuuAndErpsatus(Long enuu, Integer status) {
        return dao.findByEnuuAndErpstatus(enuu, status);
    }

    /**
     * 更新下载状态
     * @param idStr
     */
    @Override
    public void updateDownStatus(String idStr) {
        String sql = "update customer set cu_erpstatus = 203 where cu_id in (" + idStr + ")";
        jdbcTemplate.execute(sql);
    }

    /***
     * 找到最新的客户资料表
     * @param enuu
     * @return
     */
    @Override
    public CustomerInfo findNew(Long enuu) {
        List<CustomerInfo> infos = dao.findByEnuuOrderBySubmitTimeDesc(enuu);
        return infos.size() > 0 ? infos.get(0) : null;
    }

    /**
     * 找到客户资料表
     * @param id
     * @return
     */
    @Override
    public CustomerInfo findOne(Long id) {
        return dao.findOne(id);
    }

}
