package com.ruoyi.app.controller.common;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.JWTUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.system.domain.AppLogin;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.dto.ContractorUserDTO;
import com.ruoyi.system.service.IAppLoginService;
import com.ruoyi.system.service.IContractorUserService;
import com.ruoyi.system.service.ISysNoticeService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 公告 信息操作处理
 *
 * @author ruoyi
 */
@Api(tags = "APP通知、公告、个人消息")
@RestController
@RequestMapping("/app/system/notice")
public class SysNoticeAppController extends BaseController {
    @Autowired
    private ISysNoticeService noticeService;

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IAppLoginService appLoginService;
    @Autowired
    private IContractorUserService contractorUserService;

    /**
     * 获取通知公告列表
     */
    @ApiOperation("查询通知公告消息")
    @GetMapping("/list")
    public TableDataInfo list() {
        SysNotice notice = new SysNotice();
        List<String> typeList = new ArrayList<>(2);
        typeList.add("1");
        typeList.add("2");
        notice.setNoticeTypeIn(typeList);

//        PageDomain pageDomain = TableSupport.buildPageRequest();
//        pageDomain.setIsAsc("desc");
//        pageDomain.setOrderByColumn("create_time");
//        startOrderBy();
        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    /**
     * 获取通知公告列表
     */
    @ApiOperation("查询个人消息（待审核通知、已处理通知……）")
    @GetMapping("/userMessageList")
    public TableDataInfo userMessageList(SysNotice notice, HttpServletRequest request) {
        /*String remoteAddr = request.getRemoteAddr();
        String localAddr = request.getLocalAddr();
        if (remoteAddr.startsWith("192.168.3.") || localAddr.equals("192.168.3.200") || localAddr.equals("127.0.0.1")) {
            TableDataInfo dataTable = getDataTable(new ArrayList<>());
            dataTable.setNoReadTotal(0);
            return dataTable;
        }*/

        //统计用户维度条数
        String userId = JWTUtil.getUserId();
        SysNotice noticeParam = new SysNotice();
        noticeParam.setUserId(Long.parseLong(userId));
        int countNoticeUserNoRead = noticeService.countNoticeUserNoRead(noticeParam);

        notice.setUserId(Long.parseLong(userId));
        notice.setNoticeType("3");

        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setNoReadTotal(countNoticeUserNoRead);
        return dataTable;
    }


    @ApiOperation("个人消息设置已读")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeId", value = "消息ID", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "isRead", value = "0-未读1-已读", dataType = "Integer", dataTypeClass = Integer.class)
    })
    public AjaxResult edit(@RequestBody SysNotice notice) {
        //用户昵称
        String nickName = null;
        //用户类型：1内部，2承包商
        if ("1".equals(JWTUtil.getAppUserType())) {
            //设置人员姓名
            SysUser sysUser = sysUserService.selectUserById(Long.parseLong(JWTUtil.getPCUserId()));
            if (Objects.nonNull(sysUser)) {
                nickName = sysUser.getNickName();
            }
        } else {
            AppLogin appLogin = appLoginService.selectAppLoginById(JWTUtil.getUserId());
            if (Objects.nonNull(appLogin)) {
                ContractorUserDTO contractorUserDTO = contractorUserService.selectContractorUserById(appLogin.getContractorUserId());
                if (Objects.nonNull(contractorUserDTO)) {
                    nickName = contractorUserDTO.getCompellation();
                }
            }
        }
        notice.setUpdateBy(nickName);

        notice.setStatus(null);
        notice.setNoticeContent(null);
        notice.setNoticeTitle(null);
        notice.setUserId(null);
        notice.setCreateBy(null);
        notice.setCreateTime(null);


        return toAjax(noticeService.updateNotice(notice));
    }

}
