package com.sifang.modules.insurance.web;

import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.Service.CustomerEntryService;
import com.sifang.modules.customer.Service.OperatingLeaseService;
import com.sifang.modules.customer.entity.CustomerEntry;
import com.sifang.modules.insurance.entity.CrmCustomerEntryInsurance;
import com.sifang.modules.insurance.entity.CrmCustomerNoSystemEntryInsurance;
import com.sifang.modules.insurance.service.CrmCustomerNoSystemEntryInsuranceService;
import com.sifang.modules.insurance.service.CustomerEntryInsuranceService;
import com.sifang.modules.insurance.service.InsuranceListService;
import com.sifang.modules.order.utils.WebPageFunUtils;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.setting.service.MemberService;
import com.sifang.modules.sunshine.SystemSetting.service.TraderService;
import com.sifang.modules.sunshine.SystemSetting.utils.BusinessException;
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.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.insurance.InsuranceBizService;
import com.sifang.modules.sys.web.BaseController;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
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;
import java.util.Objects;

@Controller
@RequestMapping("${adminPath}/customerEntry/noSystem/insurance")
public class CustomerNoSystemEntryInsuranceController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(CustomerNoSystemEntryInsuranceController.class);

    @Autowired
    CustomerEntryInsuranceService insuranceService;

    @Autowired
    TraderService traderService;

    @Autowired
    AddressService addressService;

    @Autowired
    MemberService memberService;

    @Autowired
    InsuranceListService insuranceListService;

    @Autowired
    InsuranceBizService insuranceBizService;

    @Autowired
    CustomerEntryService entryService;

    @Autowired
    PartnerService partnerService;

    @Autowired
    OperatingLeaseService operatingLeaseService;

    @Autowired
    CrmCustomerNoSystemEntryInsuranceService noSystemEntryInsuranceService;

    @RequestMapping(value = "toImportInsuranceNoSystem")
    public String toImportInsuranceNoSystem() {
        return "modules/insurance/importInsuranceNoSystem";
    }

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

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


    /**
     * 非本电站列表数据
     */
    @ResponseBody
    @RequestMapping(value = "insuranceListNoSystemData")
    public Object insuranceListNoSystemData(HttpServletRequest request) {
        LayuiPage layuiPage = new LayuiPage(request);
        noSystemEntryInsuranceService.list(getParams(request), layuiPage);
        return layuiPage.renderJson();
    }

    /**
     * 电站拆除标记
     * @param ids,removeSign
     * @return
     */
    @ResponseBody
    @RequestMapping("removeSign")
    public Result removeSign(@RequestParam("ids") String ids, @RequestParam("removeSign") String removeSign) {
        String[] idArr = ids.split(",");
        for(String id : idArr){
            noSystemEntryInsuranceService.removeSign(id,removeSign);
        }
        return Result.ok();
    }

    @ResponseBody
    @RequestMapping(value = "repushNoSys")
    public Result<Void> repush(@RequestParam String id) {
        CrmCustomerNoSystemEntryInsurance insurance = noSystemEntryInsuranceService.get(id);
        try {
            return noSystemEntryInsuranceService.rePushNoSystemEntryInsurance(insurance);
        } catch (BusinessException ex) {
            return Result.fail(ex.getMessage());
        } catch (Exception ex) {
            log.error("repush error", ex);
            return Result.fail("重新推送失败");
        }
    }

    /**
     * 非本系统电站推送保单到资方
     * @param id
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/pushNoSysInsuranceToYueXiu")
    public Object pushNoSysInsuranceToYueXiu(@RequestParam String id) throws IOException {
        CrmCustomerNoSystemEntryInsurance insurance = noSystemEntryInsuranceService.get(id);
        Result result = new Result();
        try {
            String code = operatingLeaseService.renewalNoSysInsurance(insurance);
            if (Objects.equals("200", code)) {
                result.setCode(code);
            } else {
                result.setCode("201");
                result.setErrMsg(code);
            }
            return result;
        } catch (BusinessException ex) {
            return Result.fail(ex.getMessage());
        } catch (Exception ex) {
            return Result.fail("重新推送失败");
        }
    }

    @RequestMapping(value = "renewalNoSysDetail/{id}")
    public String renewalNoSysDetail(@PathVariable("id") String id, Model model) {
        Record record = noSystemEntryInsuranceService.selectDetailById(id);
        model.addAttribute("insurance", record);
        model.addAttribute("processList", WebPageFunUtils.getOrderProcessDesc(record.getStr("process_detail")));
        return "modules/insurance/insuranceRenewalNoSysDetail";
    }

    /**
     * 续保文件上传
     * @param id
     * @param downUrl
     * @return
     * @throws IOException
     */
    @RequestMapping("/uploadInsuranceFile")
    public Result uploadInsuranceFile(@RequestParam String id,@RequestParam String downUrl) throws IOException {
        Result result = new Result();
        noSystemEntryInsuranceService.uploadInsuranceFile(id,downUrl);
        result.setOk();
        return result;
    }

}
