package com.sifang.modules.customer.controller;


import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.Service.AccountSetService;

import com.sifang.modules.customer.constant.AccountSetConstants;
import com.sifang.modules.customer.vo.AccountSetVo;
import com.sifang.modules.sunshine.SystemSetting.service.TraderService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.util.GuardUtils;
import com.sifang.modules.sys.web.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

// 账户设置
@Controller
@RequestMapping(value = "${adminPath}/accountSet")
public class AccountSetController extends BaseController {

    Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    private TraderService traderService;


    @Autowired
    private AccountSetService accountSetService;


    @RequestMapping(value = "accountSetPage")
    public String CustomerList(HttpServletRequest request, Model model) {
        Map<String,Record> map=accountSetService.getAccountList();
        model.addAttribute("type1",map.get(AccountSetConstants.TYPE_STAND_PRICE));
        model.addAttribute("type2",map.get(AccountSetConstants.TYPE_AMOUNT_TOP));
        model.addAttribute("type3",map.get(AccountSetConstants.TYPE_YJS_PROPORTION));
        model.addAttribute("type4",map.get(AccountSetConstants.TYPE_ZBJ_SINGLE_PRICE));
        return "modules/yangguang/systemSetting/accountSetPage";
    }

    @RequestMapping(value = "addSetAccountPage")
    public String toPsDetail(HttpServletRequest request, Model model) {
        List<Record> traders=traderService.getAllTopTrader();
        String type=request.getParameter("type");
        model.addAttribute("traders",traders);
        model.addAttribute("type",type);
        if(StringUtil.equalsAny(type, AccountSetConstants.TYPE_STAND_PRICE,AccountSetConstants.TYPE_AMOUNT_TOP)){  // 1.首次发货授信方案 2.标准发货授信方案
            List<Record> items= accountSetService.getSetDetail(type);
            model.addAttribute("items",items);
        }
        return "modules/yangguang/systemSetting/addSetAccountPage";
    }

   //结算款设置
    @RequestMapping(value = "setYjsAccountPage")
    public String setYjsAccountPage(HttpServletRequest request, Model model) {
        List<Record> traders=traderService.getAllTrader();
        String type=request.getParameter("type");
        model.addAttribute("traders",traders);
        model.addAttribute("type",type);
        List<Record> items= accountSetService.getSetDetail(type);
        model.addAttribute("items",items);
        return "modules/yangguang/systemSetting/setYjsAccountPage";
    }
    //质保金设置页面
    @RequestMapping(value = "setBzjdjSet")
    public String setBzjdjSet(HttpServletRequest request, Model model) {
        Record re = accountSetService.findZBJSinglePrice();
        String type=request.getParameter("type");
        model.addAttribute("type",type);
        model.addAttribute("apply",re);
        return "modules/yangguang/systemSetting/setBzjdjAccountPage";
    }



    //保存 标准授信额 、可授信额上限 、预结算比例 的设置
    @ResponseBody
    @RequestMapping(value = "saveAccountSet")
    public Result saveAccountSet(@RequestBody @Validated AccountSetVo setVo){
        logger.info(">>>>>>>>>>>>>>saveAccountSet setVo:{}",setVo);
        try{
            accountSetService.saveAccountSet(setVo);
        }catch (RuntimeException e){
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    //保存  质保金单价设置 的设置
    @ResponseBody
    @RequestMapping(value = "saveOtherAccountSet")
    public Result saveOtherAccountSet(HttpServletRequest request){
        String type=request.getParameter("type");
        String amount=request.getParameter("amount");
        String way = request.getParameter("way");
        GuardUtils.notBlank(amount,"结算规则为空");
        GuardUtils.notBlank(way,"结算方式为空");
        accountSetService.saveOtherAccountSet(type,amount,way);
        return Result.ok();
    }

}
