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

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.platform.fittingManager.entity.PlatformFitting;
import com.sifang.modules.platform.fittingManager.service.PlatFormFittingService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sunshine.fittingManager.dao.CrmSiteFittingDao;
import com.sifang.modules.sunshine.fittingManager.service.CrmSiteFittingApplyService;
import com.sifang.modules.sunshine.fittingManager.service.CrmSiteFittingService;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
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 com.sifang.modules.sunshine.fittingManager.service.FittingApplyService;


/**
 * @author lzp
 * @date 2019/10/11 14:28
 */
@Controller
@RequestMapping(value = "${adminPath}/site/apply")
public class CrmSiteFittingApplyController extends BaseController {

    @Autowired
    CrmSiteFittingService crmSiteFittingService;
    @Autowired
    CrmSiteFittingDao crmSiteFittingDao;
    @Autowired
    private FittingApplyService fittingApplyService;
    @Autowired
    private PlatFormFittingService platformFittingService;
    @Autowired
    private CrmSiteFittingApplyService crmSiteFittingApplyService;

    /**
     * 备件申请-待审核
     * @return
     */
    @RequestMapping("toNoauditPage")
    public String toPendingReviewPage(Model model) {
        return "modules/yangguang/fittingManager/siteApply/siteNoAudit";
    }

    /**
     * 备件申请-待审核数据
     * @return
     */
    @ResponseBody
    @RequestMapping("noAuditList")
    public Object noAuditData(HttpServletRequest request) {
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map = getParams(request);
        crmSiteFittingApplyService.getAllApplyList(page, map,"1");
        return page.renderJson();
    }


    /**
     * 备件详情页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("ToExamineForm")
    public String toFittingDetailNo(HttpServletRequest request,Model model){
        String id = request.getParameter("applyId");
        Record re = crmSiteFittingApplyService.getApplyDetail(id);
        List<Record> list = crmSiteFittingApplyService.getAllPlatFitting();
        model.addAttribute("apply", re);
        model.addAttribute("list", list);
        return "modules/yangguang/fittingManager/siteApply/applyDetail";
    }

    /**
     * 选择申请备件
     * @param request
     *
     */
    @ResponseBody
    @RequestMapping(value = "changeApply")
    public PlatformFitting  changeApply(HttpServletRequest request,Model model){
        String id= request.getParameter("id");
        PlatformFitting fitting = platformFittingService.getById(id);
        return fitting;
    }

    /**
     * 保存申请
     */
    @ResponseBody
    @RequestMapping(value = "saveApply")
    public Result saveApply(HttpServletRequest request){
        String id = request.getParameter("id");
        String fitId = request.getParameter("fitId");
        String fitCode = request.getParameter("fitCode");
        String fittingName = request.getParameter("fittingName");
        String fittingVersion = request.getParameter("fittingVersion");
        String applyNum = request.getParameter("applyNum");
        String applyMarks = request.getParameter("applyMarks");
        if(StringUtil.isBlank(id)){
            return Result.fail("202","id数据有误");
        }
        try {
            return crmSiteFittingApplyService.saveApply(id,fitId,fitCode,fittingName,fittingVersion,applyNum,applyMarks);
        }catch (Exception e ){
            e.printStackTrace();
            return Result.fail("205","审核失败");
        }
    }

    /**
     * 撤销申请
     * @param request
     */
    @ResponseBody
    @RequestMapping(value = "revokeApply")
    public Result refuseFitting(HttpServletRequest request){
        String id = request.getParameter("id");
        try {
            return crmSiteFittingApplyService.revokeApply(id);
        }catch (Exception e ){
            e.printStackTrace();
            return Result.fail("205","审核失败");
        }
    }

    /**
     * 备件申请-待审核
     * @return
     */
    @RequestMapping("toWaitReceive")
    public String toWaitReceive(Model model) {
        return "modules/yangguang/fittingManager/siteApply/waitReceive";
    }


    /**
     * 备件申请-待审核数据
     * @return
     */
    @ResponseBody
    @RequestMapping("waitReceiveList")
    public Object waitReceiveList(HttpServletRequest request) {
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map = getParams(request);
        crmSiteFittingApplyService.getAllApplyList(page, map,"3");
        return page.renderJson();
    }

    /**
     * 服务商确认入库
     */
    @ResponseBody
    @RequestMapping(value = "fittingInStock")
    public Result fittingInStock(HttpServletRequest request){
        String id = request.getParameter("id");
        String location = request.getParameter("location");
        if(StringUtil.isBlank(id)){
            return Result.fail("202","id数据有误");
        }
        try {
            return crmSiteFittingApplyService.fittingInStock(id,location);
        }catch (Exception e ){
            e.printStackTrace();
            return Result.fail("205","审核失败");
        }
    }

    /**
     * 得到库位
     */
    @ResponseBody
    @RequestMapping(value = "getLocation")
    public String getLocation(HttpServletRequest request){
        String siteId = CrmUtils.getSiteId(UserUtils.getUser());
        String id = request.getParameter("id");
        String location = crmSiteFittingDao.getLocation(id, siteId);
        return location;
    }

    /**
     * 备件申请-全部申请
     * @return
     */
    @RequestMapping("allApplyPage")
    public String allApplyPage(Model model) {
        return "modules/yangguang/fittingManager/siteApply/allApply";
    }

    /**
     * 备件申请-待审核数据
     * @return
     */
    @ResponseBody
    @RequestMapping("allApplyList")
    public Object allApplyList(HttpServletRequest request) {
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map = getParams(request);
        crmSiteFittingApplyService.getAllApplyList(page, map,null);
        return page.renderJson();
    }


    /**
     * 导出数据
     * @return
     */
    @ResponseBody
    @RequestMapping("exportsData")
    public Object exportsData(HttpServletRequest request){
        String type = request.getParameter("type");
        LayuiPage page = new LayuiPage(request);
        page.setPage(1);
        page.setLimit(10000);
        if ("1".equals(type)) {
            return fittingApplyService.getAllApplyListForExport(page, null, "1");
        } else if ("2".equals(type)) {
            return fittingApplyService.getAllApplyListForExportTw(page, null, "2");
        } else if ("3".equals(type)) {
            return fittingApplyService.getAllApplyListForExportTh(page, null, "");
        }else{
            return "201";
        }
    }

}
