package com.ghy.web.controller.order;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ghy.common.enums.WxMsgEnum;
import com.ghy.common.utils.WechatMsgUtils;
import com.ghy.customer.domain.Customer;
import com.ghy.customer.service.CustomerService;
import com.ghy.order.domain.OrderDetail;
import com.ghy.order.domain.OrderMaster;
import com.ghy.order.service.OrderDetailService;
import com.ghy.order.service.OrderMasterService;
import com.ghy.worker.domain.Worker;
import com.ghy.worker.service.WorkerService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ghy.common.annotation.Log;
import com.ghy.common.enums.BusinessType;
import com.ghy.order.domain.OrderWarnRecord;
import com.ghy.order.service.IOrderWarnRecordService;
import com.ghy.common.core.controller.BaseController;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.common.utils.poi.ExcelUtil;
import com.ghy.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 订单急报记录Controller
 *
 * @author clunt
 * @date 2024-02-18
 */
@Controller
@RequestMapping("/warn/record")
public class OrderWarnRecordController extends BaseController
{
    private String prefix = "warn/record";

    @Autowired
    private IOrderWarnRecordService orderWarnRecordService;

    @Resource
    private OrderMasterService orderMasterService;

    @Resource
    private OrderDetailService orderDetailService;

    @Resource
    private WorkerService workerService;

    @Resource
    private CustomerService customerService;

    @RequiresPermissions("warn:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 查询订单急报记录列表
     */
    @RequiresPermissions("warn:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrderWarnRecord orderWarnRecord)
    {
        startPage();
        List<OrderWarnRecord> list = orderWarnRecordService.selectOrderWarnRecordList(orderWarnRecord);
        return getDataTable(list);
    }

    /**
     * App查询订单急报记录列表
     */
    @ApiOperation("急报操作记录列表")
    @PostMapping("/app/list")
    @ResponseBody
    public TableDataInfo appList(@RequestBody OrderWarnRecord orderWarnRecord)
    {
        startPage();
        List<OrderWarnRecord> list = orderWarnRecordService.selectOrderWarnRecordList(orderWarnRecord);
        return getDataTable(list);
    }

    @ApiOperation("新增急报记录(约单/催单)")
    @PostMapping("/app/add")
    @ResponseBody
    public AjaxResult appAdd(@RequestBody OrderWarnRecord orderWarnRecord)
    {
        Worker worker;
        Customer customer;
        if("01".equals(orderWarnRecord.getOrderType())){
            OrderMaster orderMaster = orderMasterService.selectById(orderWarnRecord.getOrderId());
            worker = workerService.selectById(orderMaster.getWorkerId());
            customer = customerService.selectByCustomerId(orderMaster.getCustomerId());
        }else {
            OrderDetail orderDetail = orderDetailService.selectById(orderWarnRecord.getOrderId());
            worker = workerService.selectById(orderDetail.getWorkerId());
            customer = customerService.selectByCustomerId(orderDetail.getCustomerId());
        }
        // 发送急报
        // 推送公众号通知数据。
        try {
            // 消息组装。
            Map<String, Object> params = new HashMap<>();
            // 订单编号
            params.put("thing3", customer.getName());
            // 名称
            params.put("thing2", orderWarnRecord.getContent());
            // 反馈时间
            params.put("time6", com.ghy.common.utils.DateUtils.parseDateToStr("yyyy年MM月dd日 HH:mm", new Date()));
            // 消息推送
            WechatMsgUtils.sendWeChatMsg(WechatMsgUtils.getToken(), worker.getWxOpenId(), WxMsgEnum.WARN_ORDER, params);
        } catch (Exception e) {
            // 暂时不做任何操作。
            logger.error(e.getMessage(), e);
        }
        return toAjax(orderWarnRecordService.insertOrderWarnRecord(orderWarnRecord));
    }


    /**
     * 导出订单急报记录列表
     */
    @RequiresPermissions("warn:record:export")
    @Log(title = "订单急报记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OrderWarnRecord orderWarnRecord)
    {
        List<OrderWarnRecord> list = orderWarnRecordService.selectOrderWarnRecordList(orderWarnRecord);
        ExcelUtil<OrderWarnRecord> util = new ExcelUtil<OrderWarnRecord>(OrderWarnRecord.class);
        return util.exportExcel(list, "订单急报记录数据");
    }

    /**
     * 新增订单急报记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存订单急报记录
     */
    @RequiresPermissions("warn:record:add")
    @Log(title = "订单急报记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OrderWarnRecord orderWarnRecord)
    {
        return toAjax(orderWarnRecordService.insertOrderWarnRecord(orderWarnRecord));
    }

    /**
     * 修改订单急报记录
     */
    @RequiresPermissions("warn:record:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        OrderWarnRecord orderWarnRecord = orderWarnRecordService.selectOrderWarnRecordById(id);
        mmap.put("orderWarnRecord", orderWarnRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单急报记录
     */
    @RequiresPermissions("warn:record:edit")
    @Log(title = "订单急报记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OrderWarnRecord orderWarnRecord)
    {
        return toAjax(orderWarnRecordService.updateOrderWarnRecord(orderWarnRecord));
    }

    /**
     * 删除订单急报记录
     */
    @RequiresPermissions("warn:record:remove")
    @Log(title = "订单急报记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(orderWarnRecordService.deleteOrderWarnRecordByIds(ids));
    }
}
