package com.sifang.modules.insurance.web;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.Service.CustomerEntryService;
import com.sifang.modules.customer.entity.CustomerEntry;
import com.sifang.modules.insurance.entity.CrmCustomerEntryInsurance;
import com.sifang.modules.insurance.service.CustomerEntryInsuranceService;
import com.sifang.modules.platform.service.AddressService;
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.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 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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("${adminPath}/insurance/push")
public class InsurancePushController extends BaseController {

    @Autowired
    CustomerEntryInsuranceService insuranceService;
    @Autowired
    TraderService traderService;
    @Autowired
    AddressService addressService;
    @Autowired
    InsuranceBizService insuranceBizService;
    @Autowired
    CustomerEntryService customerEntryService;

    private static final Logger log = LoggerFactory.getLogger(InsurancePushController.class);
    /**
     * 进件列表
     */
    @RequestMapping(value = "list")
    public String list(HttpServletRequest request, Model model) {
        User user = UserUtils.getUser();
        model.addAttribute("user", user);
        //代理商列表
        List<Record> traderList = traderService.getTopTrader();
        model.addAttribute("traderList", traderList);
        List<Record> pros = addressService.getProvinces();
        model.addAttribute("province", pros);
        return "modules/insurance/insurancePushList";
    }

    @ResponseBody
    @RequestMapping(value = "repush")
    public Result<Void> repush(@RequestParam String id) {
        CrmCustomerEntryInsurance insurance = insuranceService.get(id);
        CustomerEntry entry = customerEntryService.getById(insurance.getEntryId());
        try {
            return insuranceBizService.repush(entry,insurance);
        } catch (BusinessException ex) {
            return Result.fail(ex.getMessage());
        } catch (Exception ex) {
            log.error("repush error", ex);
            return Result.fail("重新推送失败");
        }
    }

    /**
     * 批量推送续保
     */
    @ResponseBody
    @RequestMapping(value = "pushInsures")
    public Result pushInsures(@RequestParam String ids) {
        Result result = null;
        String[] idList = ids.split(",");
        for (String id : idList) {
            CrmCustomerEntryInsurance insurance = insuranceService.get(id);
            CustomerEntry entry = customerEntryService.getById(insurance.getEntryId());
            try {
                result = insuranceBizService.repush(entry,insurance);
            } catch (BusinessException ex) {
                log.info("pushInsures error", ex.getMessage());
                return Result.fail(ex.getMessage());
            } catch (Exception ex) {
                log.error("pushInsures error", ex);
                return Result.fail("推送失败");
            }
        }
        return result;
    }

    /**
     * 保险公司信息列表数据
     */
    @ResponseBody
    @RequestMapping(value = "insuranceData")
    public Object insuranceListData(HttpServletRequest request) {
        LayuiPage layuiPage = new LayuiPage(request);
        insuranceService.pushList(getParams(request), layuiPage);
        return layuiPage.renderJson();
    }

}
