package com.happy.action;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.happy.Model.*;
import com.happy.Until.GetHttpParam;
import com.happy.Until.ResUtil;
import com.happy.Until.UUIDUtil;
import com.happy.dto.IPage;
import com.happy.service.*;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class WorkflowAction  extends ActionSupport implements ServletRequestAware {

    private HttpServletRequest request;
    public HttpServletResponse response;

    public String id;//流程id
    public String hotelId;//民宿id
    public Integer status; //状态 1、正在审批 2、审批通过3、拒绝
    public Integer page = 1; // 当前页
    public Integer rows = 10;// 每页显示的行数rows

    @Resource
    public ArticleTweetService articleTweetService;
    @Resource
    public WorkflowService workflowService;
    @Resource
    public FileService fileService;
    @Resource
    public HotelService hotelService;
    @Resource(name = "BookingCommentService")
    private BookingCommentService bookingCommentService;
    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    /**
     * 民宿审批接口
     * @return
     */
    public String workflow(){
        JSONObject resultJson = new JSONObject();
        Gson gson = new Gson();
        com.alibaba.fastjson.JSONObject json = GetHttpParam.getRequestParameters(request);
        if (json == null) {
            resultJson.put("message", "请传入参数");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        Workflow workflow = new Workflow();
        try{
            workflow = gson.fromJson(json.toString(), new TypeToken<Workflow>() {}.getType());
            if(workflow==null){
                resultJson.put("message", "数据为空");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            if(workflow.getId()==null){
                resultJson.put("message", "流程id不能为空！");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            if(workflow.getStatus()==null){
                resultJson.put("message", "流程状态不能为空！");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            if(workflow.getType()==null){
                resultJson.put("message", "流程类型不能为空！");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            if(workflow.getWorkflowRemark()==null){
                resultJson.put("message", "审批备注不能为空！");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            int m = 0;
            Workflow workflow1 = workflowService.queryById(workflow.getId());
            if(workflow1.getStatus().intValue() != 1){
                resultJson.put("message", "该流程已审批，请勿重复操作！");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            workflow1.setWorkflowRemark(workflow.getWorkflowRemark());//审批备注
            workflow1.setWorkflowDate(UUIDUtil.getNewDate());//审批时间
            workflow1.setWorkflowName("admin");//审批人名称
            switch (workflow.getType()){
                //民宿信息修改流程审批
                case 1 : {
                    if (workflow.getStatus() == 2){//审批通过
                        // 新的酒店图片
                        List<FileInfo> fileInfoList = fileService.queryList("and link_id ='"+workflow1.getId()+"'");
                        if (fileInfoList != null && fileInfoList.size() > 0){
                            fileService.delLinkFile(String.valueOf(workflow1.getLinkId()));
                            fileInfoList.forEach(file -> {
                                file.setLinkId(workflow1.getLinkId());
                                fileService.updateFile(file);
                            });
                        }
                        workflow1.setStatus(2);//审批通过
                        //民宿信息修改
                        Hotel hotel = hotelService.getByManagerId(Integer.parseInt(workflow1.getLinkId()));
                        boolean isUpdateHotel = false;
                        if (workflow1.getCoverImg() != null && !"".equals(workflow1.getCoverImg())){
                            hotel.setCoverImg(workflow1.getCoverImg());
                            isUpdateHotel = true;
                        }
                        if (workflow1.getRemark() != null && !"".equals(workflow1.getRemark())){
                            hotel.setRemark(workflow1.getRemark());
                            isUpdateHotel = true;
                        }
                        if (isUpdateHotel){
                            hotelService.updateHotel(hotel);
                        }
                    }else {//审批拒绝
                        workflow1.setStatus(3); //拒绝审批
                    }
                    m = workflowService.update(workflow1);
                } break;
                case 2: {
                    BookingComment bookingComment = bookingCommentService.queryById(workflow1.getLinkId());
                    if (workflow.getStatus() == 2){//审批通过
                        workflow1.setStatus(2);
                        bookingComment.setStatus("1");
                    }else {//审批拒绝
                        workflow1.setStatus(3);
                        bookingComment.setStatus("3");
                    }
                    int n = bookingCommentService.update(bookingComment);
                    m = workflowService.update(workflow1);
                } break;
                //region
                //2023-10-11 A-jax添加推文的审批操作
                case 3: {
                    ArticleTweet articleTweet = articleTweetService.queryArticleById(workflow1.getLinkId(), null);
                    workflow1.setStatus(workflow.getStatus());
                    articleTweet.setApprove(workflow.getStatus());
                    int n = articleTweetService.updateArticleApprove(articleTweet);
                    m = workflowService.update(workflow1);

                }
                //endregion
            }
            if (m > 0) {
                resultJson.put("message", "审批成功");
                resultJson.put("code", 200);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            } else {
                resultJson.put("message", "审批失败");
                resultJson.put("code", 502);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
            resultJson.put("message", "未知异常："+ e);
            resultJson.put("code", 205);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        }
        return null;
    }

    public String queryById(){
        JSONObject resultJson = new JSONObject();
        if (id == null){
            resultJson.put("message", "流程id不能为空！");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        Workflow workflow = workflowService.queryById(id);
        if (workflow != null){
            //民宿信息维护流程
            List<FileInfo> fileInfoList;
            if (workflow.getStatus() == 2 && workflow.getType() == 1){
                fileInfoList = fileService.queryList("and link_id = '"+workflow.getLinkId()+"'");
            }
            //region
            //2023-10-11 A-jax添加推文流程数据获取逻辑
            else if(workflow.getType() == 3){
                fileInfoList = articleTweetService.queryList("and link_id = '"+workflow.getLinkId()+"'");
            }
            //endregion
            else {
                fileInfoList = fileService.queryList("and link_id = '"+workflow.getId()+"'");
            }
            workflow.setFileInfoList(fileInfoList);
            resultJson.put("message", "查询成功");
            resultJson.put("code", 200);
            resultJson.put("data", workflow);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        resultJson.put("message", "未查询到流程信息！");
        resultJson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    /**
     * 商户查看接口
     */
    public String hotelWorkflowList(){
        JSONObject resultJson = new JSONObject();
        StringBuilder s1 = new StringBuilder("");
        if (id == null){
            resultJson.put("message", "酒店id不能为空！");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        s1.append(" and a.link_id = ").append(id).append(" ");
        if (status != null){
            s1.append(" and a.status = ").append(status).append(" ");
        }
        IPage<Workflow> list = workflowService.queryList(s1.toString(), page, rows);
        resultJson.put("message", "查询成功");
        resultJson.put("code", 200);
        resultJson.put("data", list);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }


    /**
     * 管理端代办接口
     */
    public String queryWorkflow(){
        JSONObject resultJson = new JSONObject();
        StringBuilder s1 = new StringBuilder("");
        s1.append(" and a.status = 1 ");
        IPage<Workflow> list = workflowService.queryList(s1.toString(), page, rows);
        resultJson.put("message", "查询成功");
        resultJson.put("code", 200);
        resultJson.put("data", list);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    /**
     * 管理端已审核接口
     */
    public String queryApprovedWorkflow(){
        JSONObject resultJson = new JSONObject();
        StringBuilder s1 = new StringBuilder("");
        s1.append(" and a.status != 1 ");
        IPage<Workflow> list = workflowService.queryList(s1.toString(), page, rows);
        resultJson.put("message", "查询成功");
        resultJson.put("code", 200);
        resultJson.put("data", list);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }


}
