package com.sifang.modules.site.financial.web;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.site.financial.service.FinancialManageService;
import com.sifang.modules.sunshine.SystemSetting.dao.EmployeInformationDao;
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.util.LayuiPage;
import com.sifang.modules.sys.util.TranslationUtils;
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.List;
import java.util.Map;

@Controller
@RequestMapping("${adminPath}/financialManage")
public class FinancialManageController extends BaseController {

    @Autowired
    private FinancialManageService financialManageService;
    @Autowired
    EmployeInformationDao employeInformationDao;

    /**
     * 跳转服务商结算明细页面
     *
     * @return
     */
    @RequestMapping(value = "financialManageList")
    public String financialManageList(HttpServletRequest request, Model model) {
        Map<String, String> map = getParams(request);
        model.addAttribute("map", map);
        User user = UserUtils.getUser();
        String siteId = CrmUtils.getSiteId(user);
        List<Record> empList = employeInformationDao.getEmp(siteId);
        model.addAttribute("empList", empList);
        return "modules/site/financial/financialManage";
    }

    /**
     * 获取列表数据
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "listData")
    @ResponseBody
    public String listData(HttpServletRequest request) {
        LayuiPage<Record> page = new LayuiPage<>(request);
        TrimStrMap parasMap = new TrimStrMap(getParams(request));
        User user = UserUtils.getUser();
        String siteId = null;
        if (User.USER_TYPE_FWS.equals(user.getUserType())) {
            siteId = financialManageService.getSiteId(UserUtils.getUser().getId());
        } else if (User.USER_TYPE_FWSRY.equals(user.getUserType())) {
            siteId = CrmUtils.getSite(user.getId());
        }
        financialManageService.getListData(parasMap, page, siteId);
        List<Record> data = page.getData();
        for (Record r : data) {
            //   r.set("_jz_flag", TranslationUtils.transJzFlag(r.getStr("jz_flag")));
            //   r.set("_type", TranslationUtils.transJsType(r.getStr("type")));
            r.set("_warranty_type", TranslationUtils.transWarrantyType(r.getStr("warranty_type")));
            //   r.set("total_cost", financialManageService.calcOrderTotalCosts(r));
        }
        return page.renderJson();
    }

}
