/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sg.audit.material.web;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
//import com.jeeplus.modules.sg.audit.information.entity.Information;
import com.jeeplus.modules.sg.audit.information.entity.Information;
import com.jeeplus.modules.sg.audit.material.entity.Material;
import com.jeeplus.modules.sg.audit.material.service.MaterialService;
//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 com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;

/**
 * 物料详情Controller
 * @author
 * @version
 */
@Controller
@RequestMapping(value = "${adminPath}/sg/material")
public class MaterialController extends BaseController {

	@Autowired
	private MaterialService materialService;
	
	@ModelAttribute
	public Material get(@RequestParam(required=false) String id) {
		Material entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = materialService.get(id);
		}
		if (entity == null){
			entity = new Material();
		}
		return entity;
	}
	
	/**
	 * 物料详情列表页
	 */
	@RequestMapping(value = {"list", ""})
	public String list(Material material, Model model) {
		model.addAttribute("materialList", material);
		return "modules/sg/audit/material/materialList";
	}


	/**
	 * 物料详情列表数据
	 */
	@ResponseBody
	@RequestMapping(value = "data")
	public Map<String, Object> data(Material material, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Material> page = materialService.findPage(new Page<Material>(request, response), material);
		return getBootstrapData(page);
	}

	/**
	 * 物料原因类型及备注说明修改页面
	 * */
	@RequestMapping(value = "form")
	public String form(Material material, Model model) {
		model.addAttribute("Material", material);
		return "modules/sg/audit/material/materialForm";
	}


	/**
	 * 修改未完成领退料物料原因
	 * */
	@RequestMapping(value = "update")
	@ResponseBody
	public String updateInfo(Material material) {

		Material s = materialService.idFormId(material);
		material.setAfterNumber(s.getOutboundNumber());
		material.setAfterAuthor(s.getAuthorizedNumber());
		Integer count = materialService.updateInfo(material);
		Material needMaterial = new Material();
		Information upInfor = new Information();
		needMaterial.setProjectId(s.getProjectId().toString());
		Information needInfor = materialService.findNeedInfor(needMaterial);
		if (needInfor != null) {
			if (needInfor.getFirstFinish() == "" || needInfor.getFirstFinish() == null || needInfor.getFirstFinish().equals("否")) {
				upInfor.setProjectId(needInfor.getProjectId());
				upInfor.setFirstFinish("是");
				upInfor.setFirstFinishDate(new Date());
			} else  {
				upInfor.setProjectId(needInfor.getProjectId());
				upInfor.setSecondFinish("是");
				upInfor.setSecondFinishDate(new Date());
			}
			materialService.updateNeed(upInfor);
		}
		if (count > 0 ) {
			return "修改成功";
		} else {
			return "修改失败";
		}
	}


	/**
	 * 用来读取当前数据是否完成领退料-未使用
	 * */
/*	@RequestMapping(value = "findNeedBack")
	@ResponseBody
	public String findNeedBack(String id) {
		String s = materialService.findNeedBack(id);
		if (s.equals("是")) {
			return "是";
		} else {
			return "否";
		}
	}*/
/*
	*/
/**
	 * 导出物料信息excel文件
	 */

	@ResponseBody
    @RequestMapping(value = "export")
    public AjaxJson exportFile(Material material, HttpServletRequest request, HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		try {
            String fileName = "物料清单"+ DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<Material> page = materialService.findPage(new Page<Material>(request, response, -1), material);
    		new ExportExcel("物料清单", Material.class).setDataList(page.getList()).write(response, fileName).dispose();
    		j.setSuccess(true);
    		j.setMsg("导出成功！");
    		return j;
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("导出物料清单记录失败！失败信息："+e.getMessage());
		}
			return j;
    }



	

	

}