package com.uas.finance.service.Impl;

import com.uas.finance.dao.CustomerInfoDao;
import com.uas.finance.dao.FinancingApplyDao;
import com.uas.finance.model.*;
import com.uas.finance.service.FinancingApplyService;
import com.uas.finance.util.CollectionUtils;
import com.uas.finance.util.SystemSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

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

/**
 * 金融保理实现层
 * created by shicr on 2017/12/21
 **/
@Service
public class FinancingApplyServiceImpl implements FinancingApplyService {

    @Autowired
    private FinancingApplyDao financingApplyDao;

    @Autowired
    private CustomerInfoDao customerInfoDao;

    /**
     * 保存金融保理单
     *
     * @param financingApply
     * @return
     */
    @Override
    public FinancingApply save(FinancingApply financingApply) {

        //设置保存提交时间
        financingApply.setSubmitTime(new Date());
        //设置企业UU
        financingApply.setEnuu(SystemSession.getUserAccount().getSpaceUU());
        //设置保理UU
        financingApply.setFauu(10046945L);
        //设置企业名
        financingApply.setFa_enname(SystemSession.getUserAccount().getSpaceName());
        return financingApplyDao.save(financingApply);
    }

    /**
     * 根据保理UU和下载状态查找金融保理单
     *
     * @param fauu
     * @param status
     * @return
     */
    @Override
    public List<FinancingApply> findByFaenuuAndErpstatus(Long fauu, Integer status) {
        return financingApplyDao.findByFauuAndErpstatus(fauu, status);
    }

    /**
     * 根据id查找某金融保理单
     *
     * @param id
     * @return
     */
    @Override
    public FinancingApply findOne(Long id) {
        return financingApplyDao.findOne(id);
    }


    /**
     * 根据企业UU查找金融保理单
     *
     * @param enuu
     * @return
     */
    @Override
    public List<FinancingApply> findByEnuu(Long enuu) {
        return financingApplyDao.findByEnuuOrderBySubmitTimeDesc(enuu);
    }

    /**
     * 取出客户资料
     *
     * @param enuu
     */
    @Override
    public ModelMap getHistory(Long enuu){
        ModelMap map = new ModelMap();

        //取出最新的客户资料
        List<CustomerInfo> list = customerInfoDao.findByEnuuOrderBySubmitTimeDesc(enuu);
        if (!CollectionUtils.isEmpty(list)) {
            //得到最新的客户资料
            CustomerInfo customerInfo = list.get(0);
            //判断该客户资料里面是否有保理单
            FinancingApply financingApply = financingApplyDao.findByCusId(customerInfo.getId());
            //如果不存在保理单
            if(financingApply == null){
                map.put("customer", customerInfo);
                map.put("apply", null);
                map.put("status",201);
            }else{
                //获取保理单审核状态
                Integer i = financingApply.getStatus();
                //如果是已保存或者审核失败状态
                if(i==201 || i==205 ){
                    map.put("customer", customerInfo);
                    map.put("apply", financingApply);
                    map.put("status",i);
                }else if(i==202 || i==203 || i==204){
                    map.put("customer", customerInfo);
                    map.put("apply", financingApply);
                    map.put("status",i);
                }
            }
        }
        return map;
    }

    /**
     * 确认客户资料
     *
     * @param id
     * @return
     */
    @Override
    public void checkInfo(Long id) {
         financingApplyDao.checkInfo(id);
    }

    /**
     * 查找出英唐保理下的所有金融单据
     */
    @Override
    public List<FinancingApply> findByFauu(Long uu) {
       return  financingApplyDao.findByFauu(uu);
    }

    /**
     * 深拷贝客户资料
     * @param customerInfo
     * @return
     */
    public CustomerInfo copy(CustomerInfo customerInfo){
        //新增一条客户资料
        CustomerInfo customerInfo1 = new CustomerInfo(customerInfo);

        customerInfo1.setSubmitTime(new Date());
        //客户信息深拷贝
        List<AccountInfo> accountInfos = new ArrayList<AccountInfo>();
        for (AccountInfo a : customerInfo.getAccountInfoList()) {
            accountInfos.add(a);
        }
        customerInfo1.setAccountInfoList(accountInfos);

        List<AssociateCompany> associateCompanies = new ArrayList<AssociateCompany>();
        for (AssociateCompany a : customerInfo.getAssociateCompanies()) {
            associateCompanies.add(a);
        }
        customerInfo1.setAssociateCompanies(associateCompanies);

        BusinessCondition businessCondition = new BusinessCondition();

        List<ChangesInstruction> changesInstructions = new ArrayList<ChangesInstruction>();
        for (ChangesInstruction a : customerInfo.getChangesInstructions()) {
            changesInstructions.add(a);
        }
        customerInfo1.setChangesInstructions(changesInstructions);

        List<CustomerExcutive> excutives = new ArrayList<CustomerExcutive>();
        for (CustomerExcutive a : customerInfo.getCustomerExcutives()) {
            excutives.add(a);
        }
        customerInfo1.setCustomerExcutives(excutives);

        List<MFCustInfo> mfCustInfos = new ArrayList<MFCustInfo>();
        for (MFCustInfo m : customerInfo.getMfCustInfos()) {
            mfCustInfos.add(m);
        }
        customerInfo1.setMfCustInfos(mfCustInfos);

        List<Shareholders> shareholders = new ArrayList<Shareholders>();
        for (Shareholders s : customerInfo.getShareholders()) {
            shareholders.add(s);
        }
        customerInfo1.setShareholders(shareholders);

        List<ProductMix> productMixes = new ArrayList<ProductMix>();
        for (ProductMix p : customerInfo.getProductMixes()) {
            productMixes.add(p);
        }
        customerInfo1.setProductMixes(productMixes);

        List<Updowncast> updowncasts = new ArrayList<Updowncast>();
        for (Updowncast u : customerInfo.getUpdowncasts()) {
            updowncasts.add(u);
        }
        customerInfo1.setUpdowncasts(updowncasts);

        //保存新增的客户资料
        CustomerInfo customerInfo2 = customerInfoDao.save(customerInfo1);
        return customerInfo2;
    }

}
