package com.sifang.modules.order.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.order.service.SettlementDetailsService;
import com.sifang.modules.sys.common.utils.StringUtil;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.TranslationUtils;
import com.sifang.modules.sys.util.TrimStrMap;
import com.sifang.modules.sys.web.DjxBaseController;
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.List;
import java.util.Map;

/**
 * 平台---财务管里结算明细
 */
@Controller
@RequestMapping("${adminPath}/settlementDetails")
public class SettlementDetailsController extends DjxBaseController {
    @Autowired
    private SettlementDetailsService settlementDetailsService;
    /**
     * 结算明细列表。
     */
    @RequestMapping(value = "list")
    public String list(HttpServletRequest request, Model model) {
        Map<String, String> map = getParasMap(request);
        //月结账单跳转过来携带了参数
        model.addAttribute("map", map);
        return "modules/factory/finance/settlementDetails";
    }

    /**
     * 结算明细列表数据。
     */
    @RequestMapping(value = "listData")
    @ResponseBody
    public String listData(HttpServletRequest request) {
        LayuiPage<Record> page = new LayuiPage<>(request);
        TrimStrMap parasMap = new TrimStrMap(getParasMap(request));
        List<Record> data = page.getData();
        for (Record r : data) {
            r.set("_type", TranslationUtils.transJsType(r.getStr("type")));
            r.set("_warranty_type", TranslationUtils.transWarrantyType(r.getStr("warranty_type")));
            r.set("_prod_info", StringUtil.joinNonBlank(r.getStr("appliance_brand"), r.getStr("appliance_category")));
            r.set("total_cost", settlementDetailsService.calcOrderTotalCosts(r));
        }

        return page.renderJson();
    }



}
