package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.exception.CommonException;
import cn.cecep.talroad.common.core.exception.ServiceException;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.common.core.web.controller.BasesController;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.log.annotation.Log;
import cn.cecep.talroad.common.log.enums.BusinessType;
import cn.cecep.talroad.common.security.annotation.RequiresPermissions;
import cn.cecep.talroad.domain.BEnterpriseIrNotifierproNotice;
import cn.cecep.talroad.service.IBEnterpriseIrNotifierproNoticeService;
import cn.cecep.talroad.vo.BEnterpriseIrNotifierproNoticeVo;
import cn.cecep.talroad.vo.query.BEnterpriseIrNotifierproNoticeQuery;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.shaded.com.google.common.base.Objects;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.security.InvalidParameterException;
import java.text.ParseException;
import java.util.*;


/**
 * * 开发公司：中节能天融科技有限公司
 * 通知提醒-通知公告Controller 控制层
 *
 * @author szhpt
 * @date 2023-03-14*/
@Api(tags = "A通知提醒-通知公告")
@RestController
@RequestMapping("/notice")
public class BEnterpriseIrNotifierproNoticeController extends BasesController
{
    @Autowired
    private IBEnterpriseIrNotifierproNoticeService bEnterpriseIrNotifierproNoticeService;

    @ApiOperation(value = "查询企业通知公告类型")
    @GetMapping("/selectOptions/{factoryId}")
    public AjaxResults<List<Map<String, String>>> selectOptions(@PathVariable(value = "factoryId") String factoryId)
    {
        return AjaxResults.success(bEnterpriseIrNotifierproNoticeService.selectOptions(factoryId));
    }

    /**
     * 查询通知提醒-通知公告列表
     */
    @ApiOperation(value = "分页列表查询-通知公告")
    @GetMapping("/list")
    public AjaxResults<IPage<BEnterpriseIrNotifierproNotice>> list(@Valid BEnterpriseIrNotifierproNoticeQuery bEnterpriseIrNotifierproNotice) throws ParseException {
        Page<BEnterpriseIrNotifierproNotice> page = new Page<>(bEnterpriseIrNotifierproNotice.getPageNum(), bEnterpriseIrNotifierproNotice.getPageSize());
        IPage<BEnterpriseIrNotifierproNotice> list = bEnterpriseIrNotifierproNoticeService.page(page, BEnterpriseIrNotifierproNoticeQuery.createLambdaQueryWrapper(bEnterpriseIrNotifierproNotice));
        return AjaxResults.success(list);
    }


    /**
     * 导出通知提醒-通知公告列表
     */
    @ApiOperation(value = "导出通知提醒-通知公告")
    @Log(title = "通知提醒-通知公告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BEnterpriseIrNotifierproNotice bEnterpriseIrNotifierproNotice)
    {
        List<BEnterpriseIrNotifierproNotice> list = bEnterpriseIrNotifierproNoticeService.selectBEnterpriseIrNotifierproNoticeList(bEnterpriseIrNotifierproNotice);
        ExcelUtil<BEnterpriseIrNotifierproNotice> util = new ExcelUtil<BEnterpriseIrNotifierproNotice>(BEnterpriseIrNotifierproNotice.class);
        util.exportExcel(response, list, "通知提醒-通知公告数据");
    }

    /**
     * 获取通知提醒-通知公告详细信息
     */
    @ApiOperation(value = "单条查询-通知公告详细信息")
    @PostMapping(value = "/{id}")
    public AjaxResults<BEnterpriseIrNotifierproNotice> getInfo(@PathVariable("id") Long id) {
        return AjaxResults.success(bEnterpriseIrNotifierproNoticeService.getById(id));
    }
    /**
     * 新增通知提醒-通知公告
     */
    @ApiOperation(value = "新增通知提醒-通知公告")
    @Log(title = "通知提醒-通知公告", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody BEnterpriseIrNotifierproNotice bEnterpriseIrNotifierproNotice) {
        return toAjax(bEnterpriseIrNotifierproNoticeService.save(bEnterpriseIrNotifierproNotice));
    }
    /**
     * 修改通知提醒-通知公告
     */
    @ApiOperation(value = "修改通知提醒-通知公告")
    @Log(title = "通知提醒-通知公告", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody BEnterpriseIrNotifierproNoticeVo bEnterpriseIrNotifierproNoticeVo) {
        if(ObjectUtil.isEmpty(bEnterpriseIrNotifierproNoticeVo)){
            throw new InvalidParameterException("参数为空");
        }
        if(ObjectUtil.isEmpty(bEnterpriseIrNotifierproNoticeVo.getNoticeId())){
            throw new InvalidParameterException("参数id为空");
        }
        BEnterpriseIrNotifierproNotice bEnterpriseIrNotifierproNotice = bEnterpriseIrNotifierproNoticeService.getById(bEnterpriseIrNotifierproNoticeVo.getNoticeId());
        if(ObjectUtil.isEmpty(bEnterpriseIrNotifierproNotice)){
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", bEnterpriseIrNotifierproNoticeVo.getNoticeId()));
        }
        //赋值
        BeanUtil.copyProperties(bEnterpriseIrNotifierproNoticeVo,bEnterpriseIrNotifierproNotice);
        return toAjax(bEnterpriseIrNotifierproNoticeService.saveOrUpdate(bEnterpriseIrNotifierproNotice));
    }

    /**
     * 已读修改
     */
    @ApiOperation(value = "已读-修改状态")
    @Log(title = "已读-修改状态", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/editStatus/{noticeId}")
    public AjaxResults<Boolean> editStatus(@PathVariable(value = "noticeId") Long noticeId) {
        BEnterpriseIrNotifierproNotice entity = bEnterpriseIrNotifierproNoticeService.getBaseMapper().selectById(noticeId);
        if(entity == null){
            throw new ServiceException("数据不存在,请刷新!");
        }
        if(Objects.equal(entity.getStatus(), "1")){
            return toAjax(true);
        }
        entity.setStatus("1");
        entity.setUpdateTime(new Date());
        return toAjax(bEnterpriseIrNotifierproNoticeService.updateById(entity));
    }

    /**
     * 删除通知提醒-通知公告
     */
    @ApiOperation(value = "删除通知提醒-通知公告")
    @RequiresPermissions("talroad:notice:remove")
    @Log(title = "通知提醒-通知公告", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResults<Boolean> remove(@PathVariable Long[] ids) {
        return toAjax(bEnterpriseIrNotifierproNoticeService.removeByIds(Arrays.asList(ids)));
    }
}
