package com.huijy.web.controller.management;

import java.util.List;

import com.huijy.common.constant.Constants;
import com.huijy.management.domain.Content;
import com.huijy.system.service.ISysConfigService;
import com.huijy.weixin.service.WxUserService;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.huijy.common.annotation.Log;
import com.huijy.common.core.controller.BaseController;
import com.huijy.common.core.domain.AjaxResult;
import com.huijy.common.enums.BusinessType;
import com.huijy.management.domain.EarlyWarning;
import com.huijy.management.service.IEarlyWarningService;
import com.huijy.common.utils.poi.ExcelUtil;
import com.huijy.common.core.page.TableDataInfo;

/**
 * 预警信息Controller
 *
 * @author luobo
 * @date 2021-09-12
 */
@RestController
@RequestMapping("/management/warning")
public class EarlyWarningController extends BaseController
{
    @Autowired
    private IEarlyWarningService earlyWarningService;
    @Autowired
    private WxUserService wxUserService;
    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 查询预警信息列表
     */
    @PreAuthorize("@ss.hasPermi('management:warning:list')")
    @GetMapping("/list")
    public TableDataInfo list(EarlyWarning earlyWarning)
    {
        startPage();
        List<EarlyWarning> list = earlyWarningService.selectEarlyWarningList(earlyWarning);
        return getDataTable(list);
    }

    /**
     * 导出预警信息列表
     */
    @PreAuthorize("@ss.hasPermi('management:warning:export')")
    @Log(title = "预警信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(EarlyWarning earlyWarning)
    {
        List<EarlyWarning> list = earlyWarningService.selectEarlyWarningList(earlyWarning);
        ExcelUtil<EarlyWarning> util = new ExcelUtil<EarlyWarning>(EarlyWarning.class);
        return util.exportExcel(list, "预警信息数据");
    }

    /**
     * 获取预警信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('management:warning:query')")
    @GetMapping(value = "/{earlyWarningId}")
    public AjaxResult getInfo(@PathVariable("earlyWarningId") Long earlyWarningId)
    {
        return AjaxResult.success(earlyWarningService.selectEarlyWarningByEarlyWarningId(earlyWarningId));
    }

    /**
     * 新增预警信息
     */
    @PreAuthorize("@ss.hasPermi('management:warning:add')")
    @Log(title = "预警信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EarlyWarning earlyWarning)
    {
        earlyWarning.setCreateAt(getUserId());
        return toAjax(earlyWarningService.insertEarlyWarning(earlyWarning));
    }

    /**
     * 修改预警信息
     */
    @PreAuthorize("@ss.hasPermi('management:warning:edit')")
    @Log(title = "预警信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EarlyWarning earlyWarning)
    {
        return toAjax(earlyWarningService.updateEarlyWarning(earlyWarning));
    }

    /**
     * 删除预警信息
     */
    @PreAuthorize("@ss.hasPermi('management:warning:remove')")
    @Log(title = "预警信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{earlyWarningIds}")
    public AjaxResult remove(@PathVariable Long[] earlyWarningIds)
    {
        return toAjax(earlyWarningService.deleteEarlyWarningByEarlyWarningIds(earlyWarningIds));
    }

    @PreAuthorize("@ss.hasPermi('management:warning:send')")
    @Log(title = "发送预警", businessType = BusinessType.UPDATE)
    @PostMapping("/send")
    public AjaxResult updateSend(@RequestBody EarlyWarning earlyWarning){
        Integer num = earlyWarningService.updateSendFlag(earlyWarning.getEarlyWarningId(),earlyWarning.getSendFlag(),getUserId());
        if (num>0 && earlyWarning.getSendFlag().equalsIgnoreCase("1")){
            //先同步用户信息
            try {
                wxUserService.synchroWxUser();
            } catch (WxErrorException e) {
                e.printStackTrace();
            }
            String tempId =  sysConfigService.selectConfigByKey(Constants.WX_MP_TEMPLATE_ID_KEY);
            //发送消息
            wxUserService.sendWxTemp(earlyWarning.getEarlyWarningId(),tempId);
        }
        return AjaxResult.success();
    }

}
