package com.sifang.modules.sunshine.SystemSetting.controller;

import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.setting.service.MemberService;
import com.sifang.modules.sunshine.SystemSetting.service.SubconractorSettlementSchemeService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.util.LayuiPage;
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 org.springframework.web.multipart.MultipartFile;

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

/**
 * 结算价设置 controller
 */
@Controller
@RequestMapping(value = "${adminPath}/settlementSchemeSet")
public class SubconractorSettlementSchemeController extends BaseController {

    @Autowired
    private SubconractorSettlementSchemeService settlementSchemeService;

    @Autowired
    private MemberService memberService;

    @RequestMapping(value = "settleSetPage")
    public String settleSetPage(Model model){
        //获取所有项目信息
        model.addAttribute("project", memberService.getProjectLists());
        return "modules/yangguang/systemSetting/settleSetPage";
    }

    //列表数据
    @ResponseBody
    @RequestMapping(value = "settleSetListDate")
    public Object settleSetListDate(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        LayuiPage<Record> page = new LayuiPage<>(request);
        settlementSchemeService.getAllList(page,map);
        return page.renderJson();
    }

    //作废
    @ResponseBody
    @RequestMapping(value = "delSettleData")
    public Result delSettleData(HttpServletRequest request){
        String id=request.getParameter("id");
        settlementSchemeService.delSettleData(id);
        return Result.ok();
    }
    //终止
    @ResponseBody
    @RequestMapping(value = "stopSettleData")
    public Result stopSettleData(HttpServletRequest request){
        String id=request.getParameter("id");
        settlementSchemeService.stopSettleData(id);
        return Result.ok();
    }
    // 跳转导入页面
    @RequestMapping(value = "importSettleTheBasePriceFrom")
    public String importSettleTheBasePrice(Model model){
        return "modules/yangguang/systemSetting/importSettleTheBasePriceFrom";
    }
    // 导入
    @RequestMapping(value = "importSettleTheBasePrice")
    @ResponseBody
    public Object importSettleTheBasePrice(MultipartFile file) throws IOException {
        Map<String, String> params = Maps.newHashMap();
        params.put("fileName", StringUtil.getFileExtName(file.getOriginalFilename()));
        Map<String, Object> retMap = settlementSchemeService.importSettleTheBasePrice(params, file.getInputStream());
        return retMap;
    }
    //效验导入
    @RequestMapping(value = "checkImportSettleTheBasePrice")
    @ResponseBody
    public Object checkImportSettleTheBasePrice(MultipartFile file) throws IOException {
        Map<String, String> params = Maps.newHashMap();
        params.put("extName", StringUtil.getFileExtName(file.getOriginalFilename()));
        Map<String, Object> retMap = settlementSchemeService.checkImportSettleTheBasePrice(params, file.getInputStream());
        return retMap;
    }
}
