package com.ozs.web.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ozs.base.domain.BaseNotice;
import com.ozs.base.domain.BaseNoticeType;
import com.ozs.base.service.BaseNoticeService;
import com.ozs.base.service.BaseNoticeTypeService;
import com.ozs.base.vo.BaseNoticePageReqVo;
import com.ozs.base.vo.BaseNoticeTypePageReqVo;
import com.ozs.base.vo.BaseNoticeVo;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = ModularConstans.notice)
@RestController
@RequestMapping("/base/notice")
public class BaseNoticeController extends BaseController {


    @Autowired
    private BaseNoticeService baseNoticeService;
    @Autowired
    private BaseNoticeTypeService baseNoticeTypeService;

    @ApiOperation(value = "分页查询公告信息")
    @PostMapping("/page")
    @PreAuthorize("@ss.hasPermi('base:notice:list')")
    @Log(title = ModularConstans.notice, businessType = BusinessType.QUERY)
    public AjaxResult page(@NotEmpty(message = "数据为空")
                               @RequestBody BaseNoticePageReqVo vo) {
        LambdaQueryWrapper<BaseNotice> lw = new LambdaQueryWrapper<>();
        if(!StringUtils.isBlank(vo.getTitle())){
            lw.like(BaseNotice::getTitle,vo.getTitle());
        }
        if(!ObjectUtils.isEmpty(vo.getStartTime())){
            lw.ge(BaseNotice::getReleaseTime,vo.getStartTime());
        }
        if(!ObjectUtils.isEmpty(vo.getStartTime())){
            lw.le(BaseNotice::getReleaseTime,vo.getEntTime());
        }
        IPage<BaseNotice> page = baseNoticeService.page(new Page<>(vo.getPageNum(), vo.getPageSize()), lw);
        IPage<BaseNoticeVo> pagev = new Page<>();
        pagev.setTotal(page.getTotal());
        pagev.setCurrent(page.getCurrent());
        pagev.setPages(page.getPages());

        if(!ObjectUtils.isEmpty(page) && page.getRecords().size()>0){
            List<BaseNoticeType> list = baseNoticeTypeService.list();
            List<Long> ids = list.stream().map(BaseNoticeType::getId).collect(Collectors.toList());
            List<BaseNoticeVo> collect = page.getRecords().stream().map(o -> {
                BaseNoticeVo baseNoticeVo = new BaseNoticeVo();
                BeanUtils.copyProperties(o, baseNoticeVo);
                if (ids.contains(o.getType())) {
                    List<BaseNoticeType> collect1 = list.stream().filter(tdto -> tdto.getId().equals(o.getType())).collect(Collectors.toList());
                    if(!ObjectUtils.isEmpty(collect1)){
                        baseNoticeVo.setTypeName(collect1.get(0).getName());
                    }

                }
                return baseNoticeVo;
            }).collect(Collectors.toList());
            pagev.setRecords(collect);
        }else {
            pagev.setRecords(null);
        }
        return success(pagev);

    }



    @ApiOperation(value = "新增公告信息")
    @PostMapping("/insert")
    @PreAuthorize("@ss.hasPermi('base:notice:add')")
    @Log(title = ModularConstans.notice, businessType = BusinessType.INSERT)
    public AjaxResult insert(@NotEmpty(message = "数据为空")
                                 @RequestBody BaseNotice vo) {
        vo.setCreated(getUserId().toString());
        vo.setCreateTime(new Date());
        vo.setUpdated(vo.getCreated());
        vo.setUpdateTime(vo.getCreateTime());
        return toAjax(baseNoticeService.save(vo));
    }

    @ApiOperation(value = "删除公告信息")
    @PostMapping("/remove")
    @PreAuthorize("@ss.hasPermi('base:notice:remove')")
    @Log(title = ModularConstans.notice, businessType = BusinessType.DELETE)
    public AjaxResult remove(@NotEmpty(message = "主键id不能为空")
                             @RequestBody List<Long> ids) {
        return toAjax(baseNoticeService.removeBatchByIds(ids));
    }

    @ApiOperation(value = "查看公告信息")
    @PostMapping("/getInfo")
    @PreAuthorize("@ss.hasPermi('base:notice:query')")
    @Log(title = ModularConstans.notice, businessType = BusinessType.QUERY)
    public AjaxResult getInfo(@NotEmpty(message = "主键id不能为空")
                              @RequestParam(value = "id", required = true)
                                      Long id) {
        BaseNotice vo = baseNoticeService.getById(id);

        BaseNoticeVo baseNoticeVo = new BaseNoticeVo();
        if(!ObjectUtils.isEmpty(vo)){
            BeanUtils.copyProperties(vo, baseNoticeVo);
            List<BaseNoticeType> list = baseNoticeTypeService.list();
            List<BaseNoticeType> collect1 = list.stream().filter(tdto -> tdto.getId().equals(vo.getType())).collect(Collectors.toList());
            if(!ObjectUtils.isEmpty(collect1)){
                baseNoticeVo.setTypeName(collect1.get(0).getName());
            }
        }

        return success(baseNoticeVo);
    }
}
