package com.sptg.gateway.controller.estate;


import com.baomidou.mybatisplus.plugins.Page;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.core.constant.SysLogConstant;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.common.core.util.WebUtils;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.estate.api.constant.EstateSecurityToken;
import com.sptg.estate.api.dto.UnitDto;
import com.sptg.estate.api.dto.UserFeedbackDto;
import com.sptg.estate.api.entity.*;
import com.sptg.estate.api.feigns.MemberFeign;
import com.sptg.estate.api.feigns.UserFeedbackFeign;
import com.sptg.gateway.util.DataCheck;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.api.feigns.ConfFeign;
import com.sptg.permissions.api.feigns.OrgFeign;
import com.sptg.statistics.api.constant.SysLogMenuConstant;
import com.sptg.statistics.api.entity.SysOperationLog;
import com.sptg.statistics.api.feigns.SysOperationLogFeign;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/estate/userFeedback")
@RequiredArgsConstructor
public class UserFeedbackController {

    private final UserFeedbackFeign userFeedbackFeign;
    private final MemberFeign memberFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final ConfFeign confFeign;
    private final SysOperationLogFeign sysOperationLogFeign;
    private final FastFileStorageClient fastFileStorageClient;

    /**
     * 用户反馈管理页面
     * @return
     */
    @GetMapping(value = "/manage")
    @Security(token = EstateSecurityToken.ESTATE_USER_FEEDBACK_MANAGE,format = DataFormatEnum.JSON)
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("estate/user-feedback-manage");
        modelAndView.addObject("fastDfsServer",this.confFeign.getFileServerHost());
        return modelAndView;
    }

    /**
     * 功能描述:单元列表查询
     * @param userFeedbackDto 单元业务对象（含查询条件）
     * @param page 分页条件
     * @return  ResultBean<Page<UserFeedback>>
     *
     *
     */
    @GetMapping(value = "/pagingQuery")
    @Security(token = EstateSecurityToken.ESTATE_UNIT_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page<UserFeedback>> pagingQuery(UserFeedbackDto userFeedbackDto, Page<UserFeedback> page) {
        //根据树形选择加载数据；如果没有选中ID，则根据当前账号查询
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        userFeedbackDto.setRootOrgId(onLineUser.getRootOrgId());
        userFeedbackDto.setPage(page);
        return this.userFeedbackFeign.pagingQuery(userFeedbackDto);
    }

    /**
     * 用户反馈管理页面
     * @return modelAndView
     */
    @GetMapping(value = "/detail/{id}")
    @Security(token = EstateSecurityToken.ESTATE_USER_FEEDBACK_MANAGE,format = DataFormatEnum.JSON)
    public ModelAndView detail(@PathVariable(value = "id") Long id){
        UserFeedback userFeedback = this.userFeedbackFeign.getById(id);
        if (userFeedback == null){
            return new ModelAndView("common/404");
        }
        ModelAndView modelAndView = new ModelAndView("estate/user-feedback-detail");
        if (userFeedback.getMemberId() != null){
            Member member = this.memberFeign.getMemberByMemberId(userFeedback.getMemberId());
            if(member != null){
                modelAndView.addObject("member",member);
            }
        }
        if (userFeedback.getImageUrl() != null){
            String[] imageUrls = StringUtils.split(userFeedback.getImageUrl(),',');
            modelAndView.addObject("imageUrls",imageUrls);
        }
        modelAndView.addObject("userFeedback",userFeedback);
        modelAndView.addObject("fastDfsServer",this.confFeign.getFileServerHost());
        return modelAndView;
    }

    /**
     * 用户反馈管理页面
     * @return modelAndView
     */
    @GetMapping(value = "/handle/{id}")
    @Security(token = EstateSecurityToken.ESTATE_USER_FEEDBACK_MANAGE,format = DataFormatEnum.JSON)
    public ModelAndView handle(@PathVariable(value = "id") Long id){
        UserFeedback userFeedback = this.userFeedbackFeign.getById(id);
        if (userFeedback == null){
            return new ModelAndView("common/404");
        }
        ModelAndView modelAndView = new ModelAndView("estate/user-feedback-handle");
        if (userFeedback.getMemberId() != null){
            Member member = this.memberFeign.getMemberByMemberId(userFeedback.getMemberId());
            if(member != null){
                modelAndView.addObject("member",member);
            }
        }
        if (userFeedback.getImageUrl() != null){
            String[] imageUrls = StringUtils.split(userFeedback.getImageUrl(),',');
            modelAndView.addObject("imageUrls",imageUrls);
        }
        modelAndView.addObject("userFeedback",userFeedback);
        modelAndView.addObject("fastDfsServer",this.confFeign.getFileServerHost());
        return modelAndView;
    }

    /**
     * 功能描述:处理用户反馈
     * @param userFeedbackDto
     * @return ResultBean<Boolean>
     *
     *
     */
    @PostMapping(value = "/handleFeedback")
    @Security(token = EstateSecurityToken.ESTATE_USER_FEEDBACK_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> handleFeedback(@RequestBody UserFeedbackDto userFeedbackDto){
        //日志事件
        int operationEvent= SysLogConstant.ALTER_EVENT;
        UserFeedback oldObj = new UserFeedback();
        //新增时
        if(userFeedbackDto.getId()==null) {
            //用户
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            //机构id
            Long orgId = onLineUser.getOrgId();
            //根机构id
            Long rootOrgId = onLineUser.getRootOrgId();
            userFeedbackDto.setOrgId(orgId);
            userFeedbackDto.setRootOrgId(rootOrgId);
            operationEvent= SysLogConstant.ADD_EVENT;
            oldObj = userFeedbackDto;
        }else{
            UserFeedback dbUserFeedback = userFeedbackFeign.getById(userFeedbackDto.getId());
            if(dbUserFeedback != null){
                oldObj = dbUserFeedback;
            }
        }

        ResultBean<Boolean> resultBean = userFeedbackFeign.handle(userFeedbackDto);
        Boolean result=resultBean.getData();

        if(result != null){
            UserFeedback newObj = new UserFeedback();
            if (userFeedbackDto.getId() != null){
                newObj = this.userFeedbackFeign.getById(userFeedbackDto.getId());
            }
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            //登陆用户
            Account account = this.securityServiceInterface.getOnLineUser();
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.ESTATE_USER_FEEDBACK_MANAGE, operationEvent,
                    WebUtils.getIp(),userFeedbackDto,account,oldObj,newObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/

            return new ResultBean<>(ResultEnum.SUCCESS);
            /*------------------------------------------------------------------------------------------------------------------*/
        }
        return new ResultBean<>(ResultEnum.ERROR);
    }

    /**
     * 新增反馈
     * @param
     * @return hashMap
     */
    @PostMapping(value = "/addFeedback")
    public ResultBean<Boolean> addFeedback(@RequestBody UserFeedbackDto userFeedbackDto) {
        if (userFeedbackFeign.add(userFeedbackDto).getData()) {
            return new ResultBean<>(ResultEnum.SUCCESS);
        } else {
            return new ResultBean<>(ResultEnum.FAILURE);
        }

    }
}
