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.BEnterpriseIrBusinessAlerts;
import cn.cecep.talroad.service.IBEnterpriseIrBusinessAlertsService;
import cn.cecep.talroad.vo.BEnterpriseIrBusinessAlertsVo;
import cn.cecep.talroad.vo.query.BEnterpriseIrBusinessAlertsQuery;
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.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


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

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

    /**
     * 查询通知提醒-业务提醒列表
     */
    @ApiOperation(value = "分页列表查询-业务提醒")
    @GetMapping("/list")
    public AjaxResults<IPage<BEnterpriseIrBusinessAlerts>> list(@Valid BEnterpriseIrBusinessAlertsQuery bEnterpriseIrBusinessAlerts) throws ParseException {
        Page<BEnterpriseIrBusinessAlerts> page = new Page<>(bEnterpriseIrBusinessAlerts.getPageNum(), bEnterpriseIrBusinessAlerts.getPageSize());
        IPage<BEnterpriseIrBusinessAlerts> list = bEnterpriseIrBusinessAlertsService.page(page, BEnterpriseIrBusinessAlertsQuery.createLambdaQueryWrapper(bEnterpriseIrBusinessAlerts));
        return AjaxResults.success(list);
    }
    

    /**
     * 导出通知提醒-业务提醒列表
     */
    @ApiOperation(value = "导出通知提醒-业务提醒")
    @Log(title = "通知提醒-业务提醒", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BEnterpriseIrBusinessAlerts bEnterpriseIrBusinessAlerts)
    {
        List<BEnterpriseIrBusinessAlerts> list = bEnterpriseIrBusinessAlertsService.selectBEnterpriseIrBusinessAlertsList(bEnterpriseIrBusinessAlerts);
        ExcelUtil<BEnterpriseIrBusinessAlerts> util = new ExcelUtil<BEnterpriseIrBusinessAlerts>(BEnterpriseIrBusinessAlerts.class);
        util.exportExcel(response, list, "通知提醒-业务提醒数据");
    }

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

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

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