package org.stm.flow.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.CommonSendStatus;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.SysAnnouncement;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysAnnouncementService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.system.util.XssUtils;
import org.stm.flow.entity.FlowRunlist;
import org.stm.flow.service.IFlowRunlistService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: flow_runlist
 * @Author: jeecg-boot
 * @Date:   2025-07-04
 * @Version: V1.0
 */
@Api(tags="flow_runlist")
@RestController
@RequestMapping("/flow/flowRunlist")
@Slf4j
public class FlowRunlistController extends JeecgController<FlowRunlist, IFlowRunlistService> {
	@Autowired
	private IFlowRunlistService flowRunlistService;
	 @Autowired
	 private ISysAnnouncementService sysAnnouncementService;
	@Autowired
	private ISysUserService sysUserService;
	
	/**
	 * 分页列表查询
	 *
	 * @param flowRunlist
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "flow_runlist-分页列表查询")
	@ApiOperation(value="flow_runlist-分页列表查询", notes="flow_runlist-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<FlowRunlist>> queryPageList(FlowRunlist flowRunlist,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String username=sysUser.getUsername();
		String userid=sysUser.getId();

		flowRunlist.setStarter(username);
		QueryWrapper<FlowRunlist> queryWrapper = QueryGenerator.initQueryWrapper(flowRunlist, req.getParameterMap());
		Page<FlowRunlist> page = new Page<FlowRunlist>(pageNo, pageSize);
		IPage<FlowRunlist> pageList = flowRunlistService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param flowRunlist
	 * @return
	 */
	@AutoLog(value = "flow_runlist-添加")
	@ApiOperation(value="flow_runlist-添加", notes="flow_runlist-添加")
	@RequiresPermissions("flow:flow_runlist:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody FlowRunlist flowRunlist) {
		flowRunlistService.save(flowRunlist);
		return Result.OK("添加成功！");
	}

	 public static Date addOneMonth(Date date) {
		 Calendar calendar = Calendar.getInstance();
		 calendar.setTime(date);
		 calendar.add(Calendar.MONTH, 1);
		 return calendar.getTime();
	 }

	public static final Date getdateTimeAdd(Date dt,int addDay)
	{
		Date date=null;
		//设置日期格式
		SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.setTime(dt);
//增加一天
		c.add(Calendar.DAY_OF_MONTH, addDay);
		date=c.getTime();

		return   date;
	}

	/**
	 *  编辑
	 *
	 * @param flowRunlist
	 * @return
	 */
	@AutoLog(value = "flow_runlist-编辑")
	@ApiOperation(value="flow_runlist-编辑", notes="flow_runlist-编辑")
	//@RequiresPermissions("flow:flow_runlist:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody FlowRunlist flowRunlist) {

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String username=sysUser.getUsername();
		String userid=sysUser.getId();

		//flowRunlistService.updateById(flowRunlist);
		String cbtitle=flowRunlist.getTitle();
		String lcm=flowRunlist.getFlowname();
		String lcbt="流程催办-"+lcm;
		String MsgContent=lcm+" "+cbtitle+" </br>停留节点"+flowRunlist.getNodename()+"</br>催办理由 "+flowRunlist.getGwljson();

		Date nowd = new Date();
		Date dqrq =getdateTimeAdd(nowd,7);
		String sender=flowRunlist.getTodoemps();

		SysUser sendersysUser = sysUserService.getUserByName(sender);







		SysAnnouncement sysAnnouncement = new SysAnnouncement();
		sysAnnouncement.setTitile(lcbt);
		String title = XssUtils.scriptXss(sysAnnouncement.getTitile());
		sysAnnouncement.setTitile(title);
		// update-end-author:liusq date:20210804 for:标题处理xss攻击的问题
		sysAnnouncement.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
		//发布
		sysAnnouncement.setSendStatus(CommonSendStatus.PUBLISHED_STATUS_1);

		sysAnnouncement.setMsgContent(MsgContent);
		sysAnnouncement.setEndTime(dqrq);
		sysAnnouncement.setSender(username);
		sysAnnouncement.setPriority("H");
		sysAnnouncement.setMsgCategory("2");
		sysAnnouncement.setMsgType("USER");
		sysAnnouncement.setSendTime(new Date());
		sysAnnouncement.setMsgAbstract(title);
		sysAnnouncement.setCreateBy(username);
		sysAnnouncement.setCreateTime(new Date());
		sysAnnouncement.setUserIds(sendersysUser.getId()+",");




		sysAnnouncementService.saveAnnouncement(sysAnnouncement);

		return Result.OK("催办成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "flow_runlist-通过id删除")
	@ApiOperation(value="flow_runlist-通过id删除", notes="flow_runlist-通过id删除")
	@RequiresPermissions("flow:flow_runlist:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		flowRunlistService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "flow_runlist-批量删除")
	@ApiOperation(value="flow_runlist-批量删除", notes="flow_runlist-批量删除")
	@RequiresPermissions("flow:flow_runlist:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.flowRunlistService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "flow_runlist-通过id查询")
	@ApiOperation(value="flow_runlist-通过id查询", notes="flow_runlist-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<FlowRunlist> queryById(@RequestParam(name="id",required=true) String id) {
		FlowRunlist flowRunlist = flowRunlistService.getById(id);
		if(flowRunlist==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(flowRunlist);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param flowRunlist
    */
    @RequiresPermissions("flow:flow_runlist:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, FlowRunlist flowRunlist) {
        return super.exportXls(request, flowRunlist, FlowRunlist.class, "flow_runlist");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("flow:flow_runlist:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, FlowRunlist.class);
    }

}
