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

import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.Service.CustomerEntryService;
import com.sifang.modules.customer.entity.CustomerEntry;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmProjectContract;
import com.sifang.modules.sunshine.SystemSetting.form.AddPlan;
import com.sifang.modules.sunshine.SystemSetting.service.PlanService;
import com.sifang.modules.sunshine.SystemSetting.service.ProjectContractService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.util.GuardUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseExceptionController;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
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 javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author lzp
 * 方案管理
 */
@Controller
@RequestMapping(value = "${adminPath}/contract")
public class ProjectContractController extends BaseExceptionController {

    @Autowired
    CustomerEntryService customerEntryService;

    @Autowired
    ProjectContractService projectService;

    @RequestMapping(value = "findContractList")
    public String planList() {
        return "modules/yangguang/systemSetting/contract/contractList";
    }


    @RequestMapping(value = "toImportMaterial")
    public String toImport(){
        return "modules/yangguang/systemSetting/contract/importPlan";
    }

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

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

    @RequestMapping(value = "toAdd")
    public String toAdd(String id, String mode, Model model) {
        if (StringUtils.isNotBlank(id)) {
            Record plan = projectService.getPlanById(id);
            model.addAttribute("plan", plan);
        }
        model.addAttribute("mode", mode);
        return "modules/yangguang/systemSetting/contract/addContract";
    }


    @ResponseBody
    @RequestMapping(value = "add")
    public Result<Void> addOrEditPlan(@Valid @RequestBody CrmProjectContract req, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return errRet(bindingResult);
        }
        projectService.addOrEditPlan(req);
        return Result.ok();
    }

    @ResponseBody
    @RequestMapping(value = "delete")
    public Result<Void> deleteTrader(String ids){
        GuardUtils.notBlank(ids, "ids不能为空");
        projectService.deletePlan(ids);
        return Result.ok();
    }

    @ResponseBody
    @RequestMapping(value = "list")
    public Object getSite(HttpServletRequest request) {
        LayuiPage<Record> page = new LayuiPage<>(request);
        Map<String, String> map = getParams(request);
        projectService.listPlan(page, map);
        return page.renderJson();
    }

}
