package com.sifang.modules.changes.controller;

import com.sifang.modules.customer.Service.CustomerEntryService;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmTrader;
import com.sifang.modules.sunshine.SystemSetting.service.TraderService;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "${adminPath}/change/common")
public class ChangeCommonController extends BaseExceptionController {
    @Autowired
    TraderService traderService;
    @Autowired
    CustomerEntryService entryService;

    //选择资方页面

    /**
     * type 1.客户信息变更 2.勘测变更 3.设计变更 4.完工变更 5.并网变更 6.物料变更
     */
    @RequestMapping("/choosePartner")
    public String choosePartner(HttpServletRequest request , Model model) {
        String type = request.getParameter("type");
        // 获取代理商
        User user = UserUtils.getUser();
        CrmTrader trader;
        if (User.USER_TYPE_ADVISOR.equals(user.getUserType())) {
            String traderId = CrmUtils.getUserRelId(user);
            trader = traderService.getById(traderId);
        } else {
            trader = traderService.getTraderBuserId(user.getId());
        }
        String traderId = trader.getId();
        if ("2".equals(trader.getType())) {
            traderId = trader.getParentId();
        }
        // 获取代理商对应资方
        model.addAttribute("capital", entryService.getCapital(traderId));
        model.addAttribute("type",type);//区分哪个变更
        return "modules/yangguang/changes/common/choosePartner";
    }
}
