package com.uas.finance.controller;

import com.uas.finance.service.CustomerInfoService;
import com.uas.finance.service.FinancingApplyService;
import com.uas.finance.constants.Status;
import com.uas.finance.model.FinancingApply;
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.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 保理申请控制层
 * created by shicr on 2017/12/25
 **/
@Controller
@RequestMapping("/financeapply")
public class FinancingApplyController {

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

    @Autowired
    private FinancingApplyService service;

    @Autowired
    private CustomerInfoService customerInfoService;

    /**
     * 金融申请表单保存
     *
     * @param financingApply
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public FinancingApply save(@RequestBody FinancingApply financingApply) {
        //设置保存状态
        financingApply.setStatus(Status.SAVED.code());
        logger.info("金融申请表单保存", "success");
        return service.save(financingApply);
    }


    /**
     * 金融申请表单提交
     *
     * @param financingApply
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public FinancingApply submit(@RequestBody FinancingApply financingApply) {
        //设置成提交状态
        financingApply.setStatus(Status.SUBMIT.code());
        //轮询设置成未下载状态
        financingApply.setErpstatus(Status.NOT_UPLOAD.code());
        logger.info("金融申请表单提交", "success");
        return service.save(financingApply);
    }

    /**
     * 根据id找到金融保理单
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findOne", method = RequestMethod.GET)
    public FinancingApply findOne(@RequestBody Long id) {
        logger.info("根据id找到金融保理单","success");
        return service.findOne(id);
    }


    /**
     * 查找某企业申请的保理单
     *
     * @param enuu
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findByEnuu", method = RequestMethod.GET)
    public List<FinancingApply> findByEnuu(Long enuu) {
        logger.info("获取登录人的所有买方客户", "成功");
        if (enuu != null) {
            List<FinancingApply> financingApplies = service.findByEnuu(enuu);
            return financingApplies;
        }
        return null;
    }

    /**
     * 得到之前填写的客户资料
     *
     * @param enuu
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getHistory", method = RequestMethod.GET)
    public ModelMap hasApproved(@RequestParam(value = "enuu") Long enuu) {
        logger.info("得到之前填写的客户资料","success");
        return service.getHistory(enuu);
    }

    /**
     * 确认客户资料
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/checkinfo", method = RequestMethod.POST)
    public void checkinfo(@RequestParam  Long id){
        logger.info("确认客户资料","success");
         service.checkInfo(id);
    }

    /**
     * 查找出英唐保理下的所有客户
     */
    @ResponseBody
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public List<FinancingApply> findAll() {
        Long fauu = SystemSession.getUserAccount().getSpaceUU();
        logger.info("查找出英唐保理下的所有客户","success");
        return service.findByFauu(fauu);
    }
}
