package com.sptg.gateway.controller.estate;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.SysLogConstant;
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.NoticeDto;
import com.sptg.estate.api.entity.Notice;
import com.sptg.estate.api.entity.NoticeImg;
import com.sptg.estate.api.feigns.NoticeFeign;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.feigns.ConfFeign;
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.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * title: NoticeController
 * package: com.sptg.gateway.controller.estate
 * description: 公告管理-网关控制器
 *
 *
 *
 */
@RestController
@RequestMapping("/estate/notice")
@RequiredArgsConstructor
public class NoticeController {

    private final ConfFeign confFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final NoticeFeign noticeFeign;
    private final SysOperationLogFeign sysOperationLogFeign;

    /**
     * 功能描述:公告管理页面
     * @return ModelAndView
     *
     *
     */
    @RequestMapping("/index")
    @Security(token = EstateSecurityToken.ESTATE_NOTICE_MANAGE)
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("estate/notice-manage");
                modelAndView.addObject("fastDfsServer",this.confFeign.getFileServerHost());
        return modelAndView;
    }
    /**
     * 功能描述:展示公告列表
     * @param noticeDto
     * @param page
     * @return  ResultBean<Page<Notice>>
     *
     *
     */
    @RequestMapping("/showServerUserList")
    @Security(token = EstateSecurityToken.ESTATE_NOTICE_MANAGE)
    public ResultBean<Page<Notice>> showServerUserList(NoticeDto noticeDto, Page<Notice> page){
        //查询当前用户机构位置
        Account account=securityServiceInterface.getOnLineUser();
        noticeDto.setOrgPosition(account.getOrgPosition());
        noticeDto.setPage(page);

        return  noticeFeign.showNoticeList(noticeDto);
    }
    /**
     * 功能描述:查询公告
     * @param noticeDto
     * @return ResultBean<List<Notice>>
     *
     *
     */
    @RequestMapping("/getList")
    @Security(token = EstateSecurityToken.ESTATE_NOTICE_MANAGE)
    public ResultBean<List<Notice>> getList(NoticeDto noticeDto){
        //查询当前用户机构位置
        Account account=securityServiceInterface.getOnLineUser();
        noticeDto.setOrgPosition(account.getOrgPosition());
        return  noticeFeign.getList(noticeDto);
    }
    /**
     * 功能描述:新增页面初始化
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/add")
    @Security(token = EstateSecurityToken.ESTATE_NOTICE_ADD,format = DataFormatEnum.VIEW)
    public ModelAndView add(){
        ModelAndView modelAndView = new ModelAndView("estate/notice-add");
        modelAndView.addObject("fastDfsServer",this.confFeign.getFileServerHost());
        return modelAndView;
    }
    /**
     * 功能描述:编辑或添加公告
     * @param notice
     * @return ResultBean<Boolean>
     *
     *
     */
    @PostMapping(value = "/addOrEditNotice")
    @Security(token = EstateSecurityToken.ESTATE_NOTICE_ADD,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> addOrEditNotice(Notice notice,String [] imgUri){
        //日志事件
        int operationEvent= SysLogConstant.ALTER_EVENT;
        Notice oldObj = new Notice();
        //新增时
        if(notice.getId()==null) {
            //用户
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            notice.setCreateAccountName(onLineUser.getLoginName());
            notice.setCreateAccountId(onLineUser.getId());
            //机构id
            Long orgId = onLineUser.getOrgId();
            //根机构id
            Long rootOrgId = onLineUser.getRootOrgId();
            notice.setOrgId(orgId);
            notice.setRootOrgId(rootOrgId);
            notice.setOrgPosition(onLineUser.getOrgPosition());
            operationEvent= SysLogConstant.ADD_EVENT;
            oldObj = notice;
        }else{
            ResultBean<Notice> resultBean = this.noticeFeign.findNoticeById(notice.getId());
            if(resultBean != null && resultBean.getData() != null){
                oldObj = resultBean.getData();
            }
        }
        //添加公告图片
        if(imgUri!=null&&imgUri.length>0){
            List<NoticeImg> noticeImageList=new ArrayList<NoticeImg>();
            for (String img : imgUri) {
                    String [] imgArra=img.split(":");
                    NoticeImg noticeImage=new NoticeImg();
                    noticeImage.setImgUri(imgArra[0]);
                    if(imgArra.length==2) {
                        noticeImage.setImgMd5(imgArra[1]);
                    }
                    noticeImageList.add(noticeImage);
            }
            notice.setNoticeImages(noticeImageList);
        }
        ResultBean<Boolean> resultBean = noticeFeign.addOrEditNotice(notice);
        boolean result=resultBean.getData();
        if(result){
            Notice newObj = new Notice();
            if (notice.getId() != null){
                newObj = this.noticeFeign.findNoticeById(notice.getId()).getData();
            }
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            //登陆用户
            Account account = this.securityServiceInterface.getOnLineUser();
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.ESTATE_NOTICE_MANAGE, operationEvent,
                    WebUtils.getIp(),notice,account,oldObj,newObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }
    /**
     * 功能描述:修改/详细页面
     * @param id
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/findNoticeById/{id}")
    @Security(token = EstateSecurityToken.ESTATE_NOTICE_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView findNoticeById(@PathVariable(value = "id") long id){
        ModelAndView modelAndView = new ModelAndView("estate/notice-add");
        ResultBean<Notice> resultBean=noticeFeign.findNoticeById(id);
        modelAndView.addObject("bean",resultBean.getData());
        modelAndView.addObject("fastDfsServer",this.confFeign.getFileServerHost());
        return modelAndView;
    }
    /**
     * 功能描述:删除公告
     * @param id
     * @return ResultBean<Boolean>
     *
     *
     */
    @PostMapping(value = "/deleteNotice/{id}")
    @Security(token = EstateSecurityToken.ESTATE_NOTICE_DELETE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> deleteNotice(@PathVariable(value = "id") long id){
        Notice oldObj = this.noticeFeign.findNoticeById(id).getData();
        ResultBean<Boolean> resultBean=this.noticeFeign.deleteNoticeById(id);
        boolean result=resultBean.getData();
        if(result){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            Notice notice=new Notice();
            notice.setId(id);
            //登陆用户
            Account account = this.securityServiceInterface.getOnLineUser();
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.ESTATE_NOTICE_MANAGE, SysLogConstant.DELETE_EVENT,
                    WebUtils.getIp(),notice,account,oldObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }
    /**
     * 功能描述:批量删除公告
     * @param ids
     * @return ResultBean<Boolean>
     *
     *
     */
    @PostMapping(value = "/deleteNoticeList")
    @Security(token = EstateSecurityToken.ESTATE_NOTICE_DELETE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> deleteNoticeList(@RequestBody List<String> ids){
        if (null == ids || ids.size() <=0){
            //  参数异常
            return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY);
        }
        NoticeDto noticeDto = new NoticeDto();
        noticeDto.setIds(ids);
        ResultBean<Boolean> resultBean=this.noticeFeign.deleteNoticeList(noticeDto);
        boolean result=resultBean.getData();
        if(result) {
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            //登陆用户
            Account account = this.securityServiceInterface.getOnLineUser();
            SysOperationLog sysOperationLog = new SysOperationLog(SysLogMenuConstant.ESTATE_NOTICE_MANAGE, SysLogConstant.DELETE_EVENT,
                    WebUtils.getIp(), noticeDto, account);
            sysOperationLogFeign.info(sysOperationLog);
            /*------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

}
