package com.uas.finance.controller;

import com.alibaba.fastjson.JSON;
import com.uas.finance.model.*;
import com.uas.finance.service.*;
import com.uas.finance.util.CollectionUtils;
import com.uas.finance.util.SystemSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户基础资料表
 * created by shicr on 2017/12/18
 **/
@Controller
@RequestMapping("/customer")
public class CustomerInfoController {

    private final static Logger logger = LoggerFactory.getLogger(CustomerInfoController.class);

    @Autowired
    private CustomerInfoService service;

    @Autowired
    private CustomerExcutiveService excutiveService;

    @Autowired
    private AssociateCompanyService companyService;

    @Autowired
    private ChangesInstructionService instructionService;

    @Autowired
    private ShareholdersService shareholdersService;

    @Autowired
    private MFCustInfoService mfCustInfoService;

    @Autowired
    private BusinessConditionService businessConditionService;

    @Autowired
    private FinanceConditionService financeConditionService;

    @Autowired
    private AccountInfoService accountInfoService;

    @Autowired
    private UpdowncastService updowncastService;

    @Autowired
    private ProductMixService productMixService;

    @Autowired
    private MFCustInfoDetailService mfCustInfoDetailService;

    /**
     * 保存客户基础资料
     *
     * @param info
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save")
    public CustomerInfo save(@RequestBody CustomerInfo info) {
        info.setEnuu(SystemSession.getUserAccount().getSpaceUU());
        info.setCu_name(SystemSession.getUserAccount().getSpaceName());
        info.setSubmitTime(new Date());
        logger.info("保存客户基础资料", "success");
        return service.save(info);
    }

    /**
     * 判断是否已有客户资料
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findnew", method = RequestMethod.GET)
    public Boolean findNew() {
        CustomerInfo customerInfo = null;
        Long enuu = SystemSession.getUserAccount().getSpaceUU();
        if (enuu != null) {
            customerInfo = service.findNew(enuu);
            if (customerInfo != null) {
                return true;
            }
        }
        logger.info("查看最新客户基础资料", "success");
        return false;
    }

    /**
     * 找到某一具体的客户资料
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findone", method = RequestMethod.GET)
    public CustomerInfo findone(@RequestParam(value = "id") Long id) {
        logger.info("找到某一具体的客户资料");
        CustomerInfo customerInfo = service.findOne(id);
        return customerInfo;
    }

    /**
     * 接收UAS传来的补充客户资料
     *
     * @param customer
     */
    @Transactional
    @ResponseBody
    @RequestMapping(value = "/erp/updateinfo", method = RequestMethod.POST)
    public Map<String, List<Map<String, Long>>> updateinfo(String customer) throws UnsupportedEncodingException {

        Map<String, List<Map<String, Long>>> map = new HashMap<String, List<Map<String, Long>>>();
        //客户资料
        String customerInfo1 = URLDecoder.decode(customer, "utf-8");
        CustomerInfo customerInfo = JSON.parseObject(customerInfo1, CustomerInfo.class);
        //高管信息
        List<CustomerExcutive> customerExcutiveList = customerInfo.getCustomerExcutives();

        //股东信息表
        List<Shareholders> shareholders = customerInfo.getShareholders();
        //关联企业
        List<AssociateCompany> associateCompanies = customerInfo.getAssociateCompanies();
        //变更信息
        List<ChangesInstruction> changesInstructions = customerInfo.getChangesInstructions();

        List<Map<String, Long>> list1 = service.update(customerInfo);
        map.put("customer", list1);

        if (!CollectionUtils.isEmpty(customerExcutiveList)) {
            List<Map<String, Long>> list = excutiveService.update(customerExcutiveList);
            map.put("customerExcutive", list);
        }
        if (!CollectionUtils.isEmpty(shareholders)) {
            List<Map<String, Long>> list = shareholdersService.update(shareholders);
            map.put("shareholders", list);
        }
        if (!CollectionUtils.isEmpty(associateCompanies)) {
            List<Map<String, Long>> list = companyService.update(associateCompanies);
            map.put("associateCompany", list);
        }
        if (!CollectionUtils.isEmpty(changesInstructions)) {
            List<Map<String, Long>> list = instructionService.update(changesInstructions);
            map.put("changeInstruction", list);
        }
        logger.info("接收UAS传来的补充客户资料", "success");
        return map;
    }

    /**
     * 接收ERP传来的补充客户资料下的从表
     *
     * @param mfCust
     * @param businessCondition
     * @param prouductMixe
     * @param updowncast
     * @param financeCondition
     * @param accountList
     */
    @Transactional
    @ResponseBody
    @RequestMapping(value = "/erp/updateinfo2", method = RequestMethod.POST)
    public Map<String, List<Map<String, Long>>> updateinfo2(String mfCust,
                                                            String mfCustInfoDetailList,
                                                            String businessCondition,
                                                            String prouductMixe,
                                                            String updowncast,
                                                            String financeCondition,
                                                            String accountList)
            throws UnsupportedEncodingException {

        Map<String, List<Map<String, Long>>> map = new HashMap<String, List<Map<String, Long>>>();
        //买方客户资料
        if (mfCust != null) {
            String mfCust1 = URLDecoder.decode(mfCust, "utf-8");
            List<MFCustInfo> mfCustInfos = JSON.parseArray(mfCust1, MFCustInfo.class);
            List<Map<String, Long>> list = mfCustInfoService.update(mfCustInfos);
            map.put("mfCust", list);
        }
        //买方客户明细
        if (mfCustInfoDetailList != null) {
            String mfCustDetail = URLDecoder.decode(mfCustInfoDetailList, "utf-8");
            List<MFCustInfoDetail> mfCustInfoDetails = JSON.parseArray(mfCustDetail, MFCustInfoDetail.class);
            List<Map<String, Long>> list = mfCustInfoDetailService.update(mfCustInfoDetails);
            map.put("mfCustInfoDetailList", list);
        }
        if (businessCondition != null) {
            //贸易描述
            String businessCondition1 = URLDecoder.decode(businessCondition, "utf-8");
            BusinessCondition businessCondition2 = JSON.parseObject(businessCondition1, BusinessCondition.class);
            List<Map<String, Long>> list = businessConditionService.update(businessCondition2);
            map.put("businessCondition", list);
        }
        if (financeCondition != null) {
            //财务情况说明
            String financeCondition1 = URLDecoder.decode(financeCondition, "utf-8");
            FinanceCondition financeCondition2 = JSON.parseObject(financeCondition1, FinanceCondition.class);
            List<Map<String, Long>> list = financeConditionService.update(financeCondition2);
            map.put("financeCondition", list);
        }
        if (prouductMixe != null) {
            //经营情况表
            String prouductMixe1 = URLDecoder.decode(prouductMixe, "utf-8");
            List<ProductMix> productMixes = JSON.parseArray(prouductMixe1, ProductMix.class);
            List<Map<String, Long>> list = productMixService.update(productMixes);
            map.put("prouductMixe", list);
        }
        if (updowncast != null) {
            //上下游
            String updowncasts = URLDecoder.decode(updowncast, "utf-8");
            List<Updowncast> updowncasts1 = JSON.parseArray(updowncasts, Updowncast.class);
            List<Map<String, Long>> list = updowncastService.update(updowncasts1);
            map.put("updowncast", list);
        }
        if (accountList != null) {
            String accountInfo = URLDecoder.decode(accountList, "utf-8");
            List<AccountInfo> accountInfos = JSON.parseArray(accountInfo, AccountInfo.class);
            List<Map<String, Long>> list = accountInfoService.update(accountInfos);
            map.put("accountList", list);
        }
        logger.info("接收ERP传来的补充客户资料下的从表", "success");
        return map;
    }
}

