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


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.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
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.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.ResponseBody;

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

@Controller
@RequestMapping("${adminPath}/platform/fiting")
public class PlatFormFittingController  extends BaseController{

    @Autowired
    private PlatFormFittingService fittingService ;

    private Logger logger = LoggerFactory.getLogger(PlatFormFittingController.class);


    @RequestMapping(value = "pageList")
    public String getPlantformFittingList(HttpServletRequest request , Model model){


        return "modules/yangguang/fittingManager/fitting/plantformFittingList";
    }
    @ResponseBody
    @RequestMapping("getPlatformFittingData")
    public Object finishedFitApplyData(HttpServletRequest request) {
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map = getParams(request);
            fittingService.getPlatFormFittingPage(page,map);
        return page.renderJson();
    }

    @RequestMapping(value = "addPlatformFitting")
    public String addPlatformFitting(HttpServletRequest request , Model model){

        return "modules/yangguang/fittingManager/fitting/addPlatformFitting";
    }

    /**
     * 新增备件
     *@param fitting
     *@param request
    */
    @ResponseBody
    @RequestMapping(value = "savePlatformFitting")
    public Result savePlatformFitting(PlatformFitting fitting, HttpServletRequest request){

        User user = UserUtils.getUser();
        try {
            return fittingService.saveFitting(fitting,user);
        }catch (Exception e){
            logger.error("savePlatformFitting code :{},  Exception : {} ,user: {} ,userId: {}",fitting.getCode(),e,user,user.getId());
            e.printStackTrace();
            return  Result.fail("401","新增保存失败");
        }

    }

    /**
     * 弹出备件修改 / 盘点 页面
    */
    @RequestMapping(value = "modifyFitting")
    public String modifyFitting(HttpServletRequest request,Model model){
        String id = request.getParameter("id");
        String type = request.getParameter("type");
        PlatformFitting fitti = fittingService.getById(id);
        model.addAttribute("fitt",fitti);
        String imgs = fitti.getImg();
        int imgsCount = 0;
        if(StringUtil.isNotBlank(imgs)){
            model.addAttribute("imgs",imgs.split(","));
            String[] imgss= imgs.split(",");
            imgsCount = imgss.length;
        }
        model.addAttribute("imgsCount",imgsCount);
        if("1".equals(type)){
            //入盘点页面
            model.addAttribute("name", CrmUtils.getUserXM(UserUtils.getUser()));
            model.addAttribute("time", DateUtils.getDateTime());
            return "modules/yangguang/fittingManager/fitting/InventoryPlatformFitting";
        }
        model.addAttribute("type",type);

        return "modules/yangguang/fittingManager/fitting/modifyPlatformFitting";
    }




    /**
     * 保存修改备件信息
    */
    @ResponseBody
    @RequestMapping(value = "saveChangeFitting")
    public Result saveChangeFitting(HttpServletRequest request){
        Map<String,String> map = getParams(request);
        if(StringUtil.isBlank(map.get("id"))){
            return Result.fail("202","数据有误");
        }
        return  fittingService.updateFitting(map);
    }

    /**
     * 保存调整库存
    */
    @ResponseBody
    @RequestMapping(value = "InventoryFitting")
    public Result InventoryFitting(HttpServletRequest request){
        Map<String,String> map = getParams(request);
        if(StringUtil.isBlank(map.get("id"))){
            return Result.fail("202","数据有误");
        }
        return  fittingService.InventoryFitting(map);
    }


    /**
     * 手动入库，可以批量操作
     */
    @RequestMapping("getbatchWarehousing")
    public String getbatchWarehousing(HttpServletRequest request,Model model){
        String ids = request.getParameter("ids");
        if(StringUtil.isNotBlank(ids)){
            List<Record> list = fittingService.getPlatFitting(ids);
            model.addAttribute("list",list);
        }
        return "modules/yangguang/fittingManager/fitting/manualStorageFitting";
    }


    // 手动入库-保存
    @ResponseBody
    @RequestMapping(value = "fittingInstockSave")
    public Result<Void> fittingInstockSave(HttpServletRequest request) {
        String data = request.getParameter("data1");
        System.out.println(data);
        return fittingService.fittingInstockSave(request);
    }

    /**
     * 删除备件
    */
    @ResponseBody
    @RequestMapping(value = "PlatformfittingDelete")
    public Result<Void> PlatformfittingDelete(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        return fittingService.PlatformfittingDelete(ids);
    }




}
