package com.ghy.web.controller.order;

import com.ghy.common.annotation.Log;
import com.ghy.common.core.controller.BaseController;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.common.core.page.TableDataInfo;
import com.ghy.common.enums.BusinessType;
import com.ghy.common.utils.poi.ExcelUtil;
import com.ghy.order.domain.AfterServiceRecord;
import com.ghy.order.domain.OrderDetail;
import com.ghy.order.service.IAfterServiceRecordService;
import com.ghy.order.service.OrderDetailService;
import org.apache.commons.lang3.exception.ExceptionUtils;
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 java.util.List;
import java.util.stream.Collectors;

/**
 * 售后记录Controller
 *
 * @author clunt
 * @date 2022-09-25
 */
@Controller
@RequestMapping("/worker/record")
public class AfterServiceRecordController extends BaseController {
    private String prefix = "worker/record";

    @Autowired
    private IAfterServiceRecordService afterServiceRecordService;

    @Autowired
    private OrderDetailService orderDetailService;

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

    @PostMapping("/all/list")
    @ResponseBody
    public TableDataInfo allList(AfterServiceRecord afterServiceRecord) {
        List<AfterServiceRecord> list = afterServiceRecordService.selectAfterServiceRecordList(afterServiceRecord);
        List<Long> orderDetailIds = list.stream().map(AfterServiceRecord::getOrderDetailId).collect(Collectors.toList());

        startPage();

        return getDataTable(list);
    }

    /**
     * 查询售后记录列表
     */
    @RequiresPermissions("worker:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AfterServiceRecord afterServiceRecord) {
        startPage();
        List<AfterServiceRecord> list = afterServiceRecordService.selectAfterServiceRecordList(afterServiceRecord);
        return getDataTable(list);
    }

    /**
     * App查询售后记录列表
     */
    @PostMapping("/app/list")
    @ResponseBody
    public TableDataInfo appList(@RequestBody AfterServiceRecord afterServiceRecord) {
        startPage();
        List<AfterServiceRecord> list = afterServiceRecordService.selectAfterServiceRecordList(afterServiceRecord);
        return getDataTable(list);
    }


    /**
     * 导出售后记录列表
     */
    @RequiresPermissions("worker:record:export")
    @Log(title = "售后记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AfterServiceRecord afterServiceRecord) {
        List<AfterServiceRecord> list = afterServiceRecordService.selectAfterServiceRecordList(afterServiceRecord);
        ExcelUtil<AfterServiceRecord> util = new ExcelUtil<>(AfterServiceRecord.class);
        return util.exportExcel(list, "售后记录数据");
    }

    /**
     * 新增售后记录
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存售后记录
     */
    @Log(title = "售后记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody AfterServiceRecord afterServiceRecord) {
        logger.info("新增售后记录:{}", afterServiceRecord);
        OrderDetail orderDetail = orderDetailService.selectById(afterServiceRecord.getOrderDetailId());
        if (orderDetail.getDrawCashTime() != null) {
            return AjaxResult.error("已发起分账子单,请联系师傅处理线下售后!");
        }
        return toAjax(afterServiceRecordService.insertAfterServiceRecord(afterServiceRecord));
    }

    /**
     * 修改售后记录
     */
    @RequiresPermissions("worker:record:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        AfterServiceRecord afterServiceRecord = afterServiceRecordService.selectAfterServiceRecordById(id);
        mmap.put("afterServiceRecord", afterServiceRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存售后记录
     */
    @Log(title = "售后记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody AfterServiceRecord afterServiceRecord) {
        logger.info("修改售后记录:{}", afterServiceRecord);
        try {
            return afterServiceRecordService.updateAfterServiceRecord(afterServiceRecord);
        } catch (Exception exception) {
            logger.error(ExceptionUtils.getStackTrace(exception));
            return AjaxResult.error(exception.getMessage());
        }
    }

    /**
     * 修改商品售后记录
     */
    @PostMapping("/editGoods")
    @ResponseBody
    public AjaxResult editGoodsSave(@RequestBody AfterServiceRecord afterServiceRecord) {
        logger.info("修改商品售后记录:{}", afterServiceRecord);
        try {
            return afterServiceRecordService.updateGoodsAfterServiceRecord(afterServiceRecord);
        } catch (Exception exception) {
            logger.error(ExceptionUtils.getStackTrace(exception));
            return AjaxResult.error(exception.getMessage());
        }
    }

    /**
     * 删除售后记录
     */
//    @RequiresPermissions("worker:record:remove")
    @Log(title = "售后记录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(afterServiceRecordService.deleteAfterServiceRecordByIds(ids));
    }

    /**
     * 师傅重发/补发操作
     * 师傅端点击重发补发按钮，保存重发/补发方案
     */
    @PostMapping("/workerResendPlan")
    @ResponseBody
    public AjaxResult workerResendPlan(@RequestBody AfterServiceRecord afterServiceRecord) {
        return afterServiceRecordService.workerResendPlan(afterServiceRecord);
    }

    /**
     * 退货操作
     * 客户或师傅端进行退货操作，保存退货信息
     */
    @PostMapping("/returnGoods")
    @ResponseBody
    public AjaxResult returnGoods(@RequestBody AfterServiceRecord afterServiceRecord) {
        return afterServiceRecordService.returnGoods(afterServiceRecord);
    }

    /**
     * 师傅确认收货
     * 师傅确认收到货物后，根据同意处理方式决定是否执行退款
     */
    @PostMapping("/workerConfirmReceive")
    @ResponseBody
    public AjaxResult workerConfirmReceive(@RequestBody AfterServiceRecord afterServiceRecord) {
        return afterServiceRecordService.workerConfirmReceive(afterServiceRecord);
    }
}
