package com.jeeplus.modules.sg.managementcenter.reserveManagement.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.act.service.ActProcessService;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.filing.basicInformation.entity.BasicInformation;
import com.jeeplus.modules.sg.managementcenter.project.service.ProjectService;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.entity.PersonnelTurnover;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.entity.ProgressSchedule;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.entity.ReserveProcess;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.service.ProgressScheduleService;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.service.ReserveProcessService;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
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 javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 人员流转 Controller
 */
@Controller
@RequestMapping(value = "${adminPath}/sg/managementCenter/progressSchedule")
public class ProgressScheduleController extends BaseController {

    @Autowired
    private ProgressScheduleService service;


    @ModelAttribute
    public ProgressSchedule get(@RequestParam(required = false) String id) {
        ProgressSchedule entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = service.get(id);
        }
        if (entity == null) {
            entity = new ProgressSchedule();
        }
        return entity;
    }

    /**
     * 人员流转信息列表页面
     * @param progressSchedule
     * @param model
     * @return
     */
    @RequiresPermissions("sg:reserve:progressSchedule:list")
    @RequestMapping(value = "list")
    public String skipList(ProgressSchedule progressSchedule, Model model) {
        model.addAttribute("progressSchedule", progressSchedule);
        return "modules/sg/managementcenter/reserveManagement/progressScheduleList";
    }

    /**
     * 人员流转信息数据列表
     * @param progressSchedule
     * @param request
     * @param response
     * @param model
     * @return
     */
    @ResponseBody
    @RequiresPermissions("sg:reserve:progressSchedule:list")
    @RequestMapping(value = "data")
    public Map<String, Object> basicData(ProgressSchedule progressSchedule, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ProgressSchedule> page = service.findPage(new Page<ProgressSchedule>(request, response), progressSchedule);
        List<ProgressSchedule> list = page.getList();
        for (ProgressSchedule p :
                list) {
            String addWareHousing = p.getAddWareHousing();
            if (null != addWareHousing && !"".equals(addWareHousing)) {
                if ("0".equals(addWareHousing)) {
                    p.setAddWareHousing("入库前");
                }
                if ("1".equals(addWareHousing)) {
                    p.setAddWareHousing("已入库");
                }
                if ("2".equals(addWareHousing)) {
                    p.setAddWareHousing("入库退回");
                }
                if ("3".equals(addWareHousing)) {
                    p.setAddWareHousing("入库退回再入库");
                }
            }
        }
        return getBootstrapData(page);
    }

    /**
     * 保存表单
     * @param progressSchedule
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "save")
    public AjaxJson save(ProgressSchedule progressSchedule){
        AjaxJson j = new AjaxJson();
        /**
         * 后台hibernate-validation插件校验
         */
        String errMsg = beanValidator(progressSchedule);
        if (StringUtils.isNotBlank(errMsg)){
            j.setSuccess(false);
            j.setMsg(errMsg);
            return j;
        }
        //新增或编辑表单保存
        //通过储备项目编号
        service.save(progressSchedule);//保存
        j.setSuccess(true);
        j.setMsg("保存人员流转信息成功");
        return j;
    }

    /**
     * 通过项目id重置项目
     * */
    @ResponseBody
    @RequestMapping(value = "reset")
    public AjaxJson reset(ProgressSchedule progressSchedule){
        AjaxJson j = new AjaxJson();
        String reset = service.reset(progressSchedule);//重置
        j.setSuccess(true);
        j.setMsg(reset);
        return j;
    }
}
