package com.sifang.modules.rechargewallet.web;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.order.dao.SiteSettlementRecordDao;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.TrimStrMap;
import com.sifang.modules.sys.web.BaseController;
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 org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
@RequestMapping(value = "${adminPath}/site/financialManagement")
public class SiteFinancialManagementController extends BaseController {
    @Autowired
    SiteSettlementRecordDao siteSettlementRecordDao;

    /***
     * 服务商端结算管理的结算明细页面
     * @return
     */
    @RequestMapping(value = "billingDetailPage")
    public String toBillingDetailPage(HttpServletRequest request, Model model) {
        Map<String, String> map = getParams(request);
        String site_billing_id = map.get("site_billing_id");
        if (StringUtil.isNotBlank(map.get("site_billing_id"))) {
            model.addAttribute("site_billing_id", site_billing_id);
        }
        return "modules/site/financialManagement/billingDetailPage";
    }

    /**
     * 服务商端结算管理的结算明细页面中的数据
     */
    @ResponseBody
    @RequestMapping(value = "billingDetailList")
    public Object getBillingDetailList(HttpServletRequest request) {
        Map<String, String> map = getParams(request);
        String siteId = CrmUtils.getUserRelId(UserUtils.getUser());
        map.put("siteId", siteId);
        TrimStrMap parasMap = new TrimStrMap(map);
        LayuiPage page = new LayuiPage(request);
        siteSettlementRecordDao.listData(parasMap, page);
        return page.renderJson();
    }

    /***
     * 服务商端结算管理的结算明细页面中的汇总数据
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "summaryData")
    public Object getSummaryData(HttpServletRequest request) {
        Map<String, String> map = getParams(request);
        String siteId = CrmUtils.getUserRelId(UserUtils.getUser());
        map.put("siteId", siteId);
        TrimStrMap parasMap = new TrimStrMap(map);
        Record record = siteSettlementRecordDao.getTotalCosts(parasMap);
        return record;
    }

}
