package com.sifang.modules.site.fittingManager.web;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.order.entity.CrmOrder;
import com.sifang.modules.order.service.OrderService;
import com.sifang.modules.site.fittingManager.service.SiteFittingApplyService;
import com.sifang.modules.site.fittingManager.service.SiteFittingRefundService;
import com.sifang.modules.sunshine.SystemSetting.dao.EmployeInformationDao;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sunshine.fittingManager.entity.CrmPlantformFittingRefundMsg;
import com.sifang.modules.sunshine.fittingManager.entity.CrmPlatformFittingRefund;
import com.sifang.modules.sunshine.fittingManager.service.FittingApplyService;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
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.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
@RequestMapping(value = "${adminPath}/service/fitting")
public class SiteFittingManagerController extends BaseController {
    @Autowired
    SiteFittingApplyService siteFittingApplyService;
    @Autowired
    private FittingApplyService fittingApplyService;

    @Autowired
    SiteFittingRefundService siteFittingRefundService;
    @Autowired
    private OrderService orderService;


    /**
     * 备件管理中  备件申请的待发货页面
     */
    @RequestMapping(value = "waiteDispatch")
    public String toWaiteDispatch(Model model) {
        return "modules/site/fitting/waiteDispatchFitting";

    }

    /***
     * 备件管理中  备件申请的页面数据
     */
    @ResponseBody
    @RequestMapping(value = "getWaiteDispatchList")
    public Object getWaiteDispatchList(HttpServletRequest request) {
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map = getParams(request);
        String siteId = CrmUtils.getUserRelId(UserUtils.getUser());
        Map<String, Object> result = siteFittingApplyService.getAllApplyList(page, map, map.get("status"), siteId);
        page.setData((List<Record>) (result.get("list")));
        page.setCount((Long) (result.get("count")));
        return page.renderJson();
    }

    /**
     * 备件管理中  备件申请的在途备件页面
     */
    @RequestMapping(value = "onTheWay")
    public String toOnTheWay(Model model) {
        return "modules/site/fitting/onTheWay";
    }

    /**
     * 备件管理中  备件申请的已完成页面
     */
    @RequestMapping(value = "finishFitting")
    public String toFinishFitting(Model model) {
        return "modules/site/fitting/finishFitting";
    }

    /***
     * 弹出备件详情页面
     */
    @RequestMapping(value = "fittingDetail")
    public String toFittingDetail(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        Record re = fittingApplyService.getApplyDetail(id);
        model.addAttribute("applyRecord", re);
        String imgs = re.getStr("fitting_img");
        if (StringUtils.isNotBlank(imgs)) {
            model.addAttribute("fitImgs", imgs.split(","));
            model.addAttribute("imgsCount", imgs.split(",").length);
        } else {
            model.addAttribute("fitImgs", "");
            model.addAttribute("imgsCount", 0);
        }
        return "modules/site/fitting/fittingDetail";
    }

    /**
     * 删除待发货备件
     */
    @ResponseBody
    @RequestMapping(value = "deleteFitting")
    public Object toDeleteFitting(HttpServletRequest request) {
        String id = request.getParameter("id");
        String creator = CrmUtils.getUserXM(UserUtils.getUser());
        Result result = new Result();
        try {
            siteFittingApplyService.delFittingAppply(id, creator);
            result.setCode("200");
        } catch (Exception e) {
            result.setCode("404");
        }
        return result;
    }

    /***
     * 在途备件确认到货
     */
    @ResponseBody
    @RequestMapping(value = "confirmReceive")
    public Object toConfirmReceive(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        String creator = CrmUtils.getUserXM(UserUtils.getUser());
        Result result = new Result();
        try {
            String code = siteFittingApplyService.confirmToTheSite(ids, creator);
            result.setCode(code);
        } catch (Exception e) {
            result.setCode("404");
        }
        return result;
    }


    @Autowired
    private EmployeInformationDao employeInformationDao;

    /***
     * 服务商端备件管理中的旧件管理页面
     */
    @RequestMapping(value = "fittingBackPage")
    public String toFittingBackPage(Model model) {
        String siteId = CrmUtils.getUserRelId(UserUtils.getUser());
        List<Record> list = employeInformationDao.getEmp(siteId);
        model.addAttribute("employees", list);
        return "modules/site/fitting/oldFittingPage";
    }

    /***
     * 获取服务商端备件管理中的旧件管理页面中的数据
     */
    @ResponseBody
    @RequestMapping(value = "getOldFittingList")
    public Object toGetFittingBackList(HttpServletRequest request) {
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map = getParams(request);
        String siteId = CrmUtils.getUserRelId(UserUtils.getUser());
        map.put("siteId", siteId);
        siteFittingRefundService.getOldFittingList(page, map);
        return page.renderJson();
    }

    /**
     * 服务商端旧件管理页面操作返厂
     */
    @ResponseBody
    @RequestMapping(value = "backOldFitting")
    public Result toBackOldFitting(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        return siteFittingRefundService.backOldFitting(ids);
    }

    /**
     * 服务商端旧件管理页面操作撤回
     */
    @ResponseBody
    @RequestMapping(value = "withdrawFitting")
    public Result toWithdrawFitting(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        return siteFittingRefundService.withdrawFitting(ids);
    }

    /***
     * 服务商端出入库明细页面
     */
    @RequestMapping(value = "toSiteInAndOutWarehousingList")
    public String toSiteInAndOutWarehousingList() {
        return "modules/yangguang/fittingManager/refundManager/siteInAndOutWarehousingList";
    }

    /***
     * 服务商端出入库明细页面数据
     */
    @ResponseBody
    @RequestMapping(value = "getSitInAndOutWarehousingData")
    public String getPlatformInAndOutWarehousingData(HttpServletRequest request) {
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map = getParams(request);
        siteFittingApplyService.getSitInAndOutWarehousingData(page, map);
        return page.renderJson();
    }

    /****
     * 返厂记录详情
     */
    @RequestMapping(value = "backRecordDetail")
    public String toBackRecordDetail(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        CrmPlatformFittingRefund fun = siteFittingRefundService.getById(id);
        model.addAttribute("refund", fun);
        CrmPlantformFittingRefundMsg msg = siteFittingRefundService.getByFunId(id);
        if (msg != null) {
            model.addAttribute("message", msg.getUpdateDetail());
        }

        if (StringUtil.isNotBlank(fun.getOrderId())) {
            CrmOrder orfde = orderService.getOrderById(fun.getOrderId());
            model.addAttribute("order", orfde);
        }
        return "modules/site/fitting/backRecordDetail";
    }

    /**
     * 修改返厂记录页面
     */
    @RequestMapping(value = "updateBackRecord")
    public String toUpdateBackRecord(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        CrmPlatformFittingRefund fun = siteFittingRefundService.getById(id);
        Date data = fun.getSendTime();
        if (data != null) {
            String sendTime = DateUtils.formatDate(data, "yyyy-MM-dd");
            model.addAttribute("sendTime", sendTime);
        }
        model.addAttribute("refund", fun);
        return "modules/site/fitting/updateBackRecord";
    }

    /***
     * 保存修改的返厂记录
     */
    @ResponseBody
    @RequestMapping(value = "saveUpdateBackRecord")
    public Object saveUpdateBackRecord(HttpServletRequest request) {
        Map<String, String> map = getParams(request);
        String siteId = CrmUtils.getUserRelId(UserUtils.getUser());
        map.put("siteId", siteId);
        String name = CrmUtils.getUserXM(UserUtils.getUser());
        map.put("update_name", name);
        Result result = siteFittingRefundService.uodateRefundFit(map);
        return result;
    }

    /***
     * 删除返厂记录
     */
    @ResponseBody
    @RequestMapping(value = "deleteBackRecord")
    public Object deleteBackRecord(HttpServletRequest request) {
        Result result = new Result();
        String ids = request.getParameter("ids");
        String creater = CrmUtils.getUserXM(UserUtils.getUser());
        if (StringUtil.isBlank(ids)) {
            result.setCode("401");
            return result;
        }
        String code = siteFittingRefundService.batchdelectOldFitting(ids, creater);
        result.setCode(code);
        return result;

    }
}
