package com.ozs.web.controller.home;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ozs.common.annotation.Log;
import com.ozs.common.constant.ModularConstans;
import com.ozs.common.core.controller.BaseController;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.enums.BusinessType;
import com.ozs.common.utils.StringUtils;
import com.ozs.home.domain.HomeNotificationMessage;
import com.ozs.home.domain.vo.HomeNotificationMessageVo;
import com.ozs.home.domain.vo.ReasonsForLagVo;
import com.ozs.home.service.HomeNotificationMessageService;
import com.ozs.pm.doman.PmDemand;
import com.ozs.pm.mapper.PmDemandMapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 首页通知消息
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/home/homeNotificationMessage")
public class HomeNotificationMessageController extends BaseController {

    @Resource
    private HomeNotificationMessageService homeNotificationMessageService;
    @Resource
    private PmDemandMapper pmDemandMapper;

    @ApiOperation(value = "查询首页通知消息")
    @PostMapping("/selectHomeNotificationMessage")
//    @PreAuthorize("@ss.hasPermi('home:homeNotificationMessage:selectHomeNotificationMessage')")
    @Log(title = ModularConstans.notice, businessType = BusinessType.QUERY)
    public AjaxResult selectHomeNotificationMessage(@RequestBody HomeNotificationMessageVo homeNotificationMessageVo) {
        LambdaQueryWrapper<HomeNotificationMessage> lw = new LambdaQueryWrapper<HomeNotificationMessage>();
        if (!StringUtils.isBlank(homeNotificationMessageVo.getHomepageClassification())) {
            lw.eq(HomeNotificationMessage::getHomepageClassification, homeNotificationMessageVo.getHomepageClassification());
        }
        if (!StringUtils.isBlank(homeNotificationMessageVo.getMessageClassification())) {
            lw.eq(HomeNotificationMessage::getMessageClassification, homeNotificationMessageVo.getMessageClassification());
        }
        IPage<HomeNotificationMessage> page = homeNotificationMessageService.page(new Page<HomeNotificationMessage>(homeNotificationMessageVo.getPageNum(), homeNotificationMessageVo.getPageSize()), lw);
        return success(page);
    }


    @ApiOperation(value = "填写滞后原因")
    @PostMapping("/saveReasonsForLag")
//    @PreAuthorize("@ss.hasPermi('home:homeNotificationMessage:saveReasonsForLag')")
    @Log(title = ModularConstans.notice, businessType = BusinessType.UPDATE)
    public AjaxResult saveReasonsForLag(@RequestBody ReasonsForLagVo reasonsForLagVo) {
        HomeNotificationMessage homeNotificationMessage = homeNotificationMessageService.getById(reasonsForLagVo.getHomeNotificationMessageId());
        homeNotificationMessage.setState(0);
        QueryWrapper<PmDemand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("demand_id", homeNotificationMessage.getDemandId());
        PmDemand pmDemand = pmDemandMapper.selectOne(queryWrapper);
        pmDemand.setDelayReason(reasonsForLagVo.getDelayReason());
        pmDemand.setAdjustAdvice(reasonsForLagVo.getAdjustAdvice());
        if (pmDemandMapper.updateById(pmDemand) > 0 && homeNotificationMessageService.updateById(homeNotificationMessage)) {
            return success();
        } else {
            return error();
        }
    }
}
