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

import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.service.LinkNumberSettingService;
import com.sifang.modules.sunshine.SystemSetting.service.RectificationSettingService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sunshine.partnerManagement.service.PartnerService;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import net.sf.json.JSONObject;
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.List;
import java.util.Map;

@Controller
@RequestMapping("${adminPath}/sysSettings/linkNumber")
public class LinkNumberSettingController extends BaseController {

    @Autowired
    private LinkNumberSettingService service;

    @Autowired
    PartnerService partnerService;

    @RequestMapping(value = "/index")
    public String toRectificationSetList(Model model) {
        model.addAttribute("paterns", partnerService.partnerList());
        return "modules/yangguang/systemSetting/bankNumberSet/rectificationSetList";
    }

    /**
     * 获取设置列表信息
     */
    @ResponseBody
    @RequestMapping(value = "getList")
    public String getList(HttpServletRequest request) {
        Map<String, String> params = getParams(request);
        LayuiPage page = new LayuiPage(request);
        service.getPageList(params,page);
        return page.renderJson();
    }

    /**
     * 到添加变更类型页面
     */
    @RequestMapping(value = "addPage")
    public String addPage(Model model) {
        model.addAttribute("paterns", partnerService.partnerList());
        return "modules/yangguang/systemSetting/bankNumberSet/addPage";
    }

    /**
     * 添加变更类型
     */
    @ResponseBody
    @RequestMapping(value="add")
    public Result add(HttpServletRequest request){
        String bankName = request.getParameter("bankName");
        String number = request.getParameter("number");
        String capitals = request.getParameter("capitals");
        Result result = new Result<>();
        try {
            return service.add(bankName, number, result, capitals);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode("205");
            result.setMsg("添加失败");
            return result;
        }
    }

    /**
     * 到修改变更类型页面
     */
    @RequestMapping(value = "editPage")
    public String editPage(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        model.addAttribute("settingInfo", service.getOneById(id));
        model.addAttribute("paterns", partnerService.partnerList());
        return "modules/yangguang/systemSetting/bankNumberSet/editPage";
    }

    /**
     * 保存修改的变更类型
     */
    @ResponseBody
    @RequestMapping(value = "edit")
    public Result edit(HttpServletRequest request) {
        String id = request.getParameter("id");
        String bankName = request.getParameter("bankName");
        String number = request.getParameter("number");
        String capitals = request.getParameter("capitals");
        Result result = new Result<>();
        try {
            return service.edit(id, bankName, number, result, capitals);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode("205");
            result.setMsg("修改失败");
            return result;
        }
    }

    /**
     * 删除变更类型
     */
    @ResponseBody
    @RequestMapping(value = "delete")
    public String delete(HttpServletRequest request) {
        String id = request.getParameter("id");
        try {
            service.deleteById(id);
            return "200";
        } catch (Exception e) {
            e.printStackTrace();
            return "405";
        }
    }

    /**
     * 导入弹框
     */
    @RequestMapping(value = "importBankNumberForm")
    public String importMaterialForm(){
        return "modules/yangguang/systemSetting/bankNumberSet/importBankNumber";
    }


    @ResponseBody
    @RequestMapping(value = "checkImportBankNumber")
    public Object checkImportBankNumber(MultipartFile file) throws IOException {
        Map<String, String> params = Maps.newHashMap();
        params.put("extName", StringUtil.getFileExtName(file.getOriginalFilename()));
        Map<String, Object> retMap = service.checkImportBankNumber(params, file.getInputStream());
        return JSONObject.fromObject(retMap);
    }


    @RequestMapping(value = "importBankNumber")
    @ResponseBody
    public JSONObject importBankNumber(MultipartFile file) throws IOException {
        Map<String, String> params = Maps.newHashMap();
        params.put("fileName", file.getOriginalFilename());
        Map<String, Object> retMap = service.importBankNumber(params, file.getInputStream());
        return JSONObject.fromObject(retMap);
    }

    /**
     * 获取联行号
     */
    @ResponseBody
    @RequestMapping(value = "findLinkNumberList")
    public Object findLinkNumberList(HttpServletRequest request){
        return service.getListByName(request.getParameter("name"),request.getParameter("partner"));
    }
}
