package com.fs.live.controller;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.domain.model.LoginUser;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ParseUtils;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.framework.web.service.TokenService;
import com.fs.his.domain.FsUser;
import com.fs.his.service.IFsUserService;
import com.fs.live.domain.LiveAfterSales;
import com.fs.live.domain.LiveAfterSalesItem;
import com.fs.live.domain.LiveAfterSalesLogs;
import com.fs.live.domain.LiveOrder;
import com.fs.live.param.LiveAfterSalesAudit1Param;
import com.fs.live.param.LiveAfterSalesAudit2Param;
import com.fs.live.param.LiveAfterSalesCancelParam;
import com.fs.live.param.LiveAfterSalesRefundParam;
import com.fs.live.service.ILiveAfterSalesItemService;
import com.fs.live.service.ILiveAfterSalesLogsService;
import com.fs.live.service.ILiveAfterSalesService;
import com.fs.live.service.ILiveOrderService;
import com.fs.live.vo.LiveAfterSalesVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * 售后记录Controller
 *
 * @author fs
 * @date 2025-07-08
 */
@RestController
@RequestMapping("/live/liveAfterSales")
public class LiveAfterSalesController extends BaseController
{
    @Autowired
    private ILiveAfterSalesService liveAfterSalesService;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private ILiveAfterSalesItemService liveAfterSalesItemService;
    @Autowired
    private ILiveAfterSalesLogsService liveAfterSalesLogsService;
    @Autowired
    private IFsUserService userService;
    @Autowired
    private ILiveOrderService orderService;

    /**
     * 获取售后记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('live:liveAfteraSales:query')")
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable("id") Long id)
    {
        LiveAfterSales liveAfterSales = liveAfterSalesService.selectLiveAfterSalesById(id);
        if(liveAfterSales==null) return R.error("售后记录不存在");
        List<LiveAfterSalesItem> list = liveAfterSalesItemService.selectLiveAfterSalesItemByAfterId(id);
        List<LiveAfterSalesLogs> logList = liveAfterSalesLogsService.selectLiveAfterSalesLogsByAfterId(id);
        FsUser user=userService.selectFsUserById(liveAfterSales.getUserId());
        user.setPhone(ParseUtils.parsePhone(user.getPhone()));
        LiveOrder liveOrder = orderService.selectLiveOrderByOrderId(String.valueOf(liveAfterSales.getOrderId()));
        return R.ok().put("afterSales",liveAfterSales).put("items",list).put("logs",logList).put("user",user).put("order",liveOrder);

    }

    /**
     * 查询售后记录列表
     */
    @PreAuthorize("@ss.hasPermi('live:liveAfteraSales:list')")
    @GetMapping("/list")
    public TableDataInfo list(LiveAfterSalesVo liveAfterSales)
    {
        startPage();
        List<LiveAfterSalesVo> list = liveAfterSalesService.selectLiveAfterSalesVoList(liveAfterSales);
        for (LiveAfterSalesVo liveAfterSalesVo : list) {
            liveAfterSalesVo.setUserPhone(ParseUtils.parsePhone(liveAfterSalesVo.getUserPhone()));
        }
        return getDataTable(list);
    }

    /**
     * 导出售后记录列表
     */
    @PreAuthorize("@ss.hasPermi('live:liveAfteraSales:export')")
    @Log(title = "售后记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LiveAfterSalesVo liveAfterSales)
    {
        PageHelper.clearPage();
        PageHelper.startPage(1, 10000, "");
        List<LiveAfterSalesVo> list = liveAfterSalesService.selectLiveAfterSalesVoList(liveAfterSales);
        for (LiveAfterSalesVo liveAfterSalesVo : list) {
            liveAfterSalesVo.setUserPhone(liveAfterSalesVo.getUserPhone() == null ? "" : liveAfterSalesVo.getUserPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            liveAfterSalesVo.setPhoneNumber(liveAfterSalesVo.getPhoneNumber() == null ? "" : liveAfterSalesVo.getPhoneNumber().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
        }
        ExcelUtil<LiveAfterSalesVo> util = new ExcelUtil<LiveAfterSalesVo>(LiveAfterSalesVo.class);
        return util.exportExcel(list, "销售直播退款订单数据");
    }


    /**
     * 新增售后记录
     */
    @PreAuthorize("@ss.hasPermi('live:liveAfteraSales:add')")
    @Log(title = "售后记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LiveAfterSales liveAfterSales)
    {
        return toAjax(liveAfterSalesService.insertLiveAfterSales(liveAfterSales));
    }

    /**
     * 修改售后记录
     */
    @PreAuthorize("@ss.hasPermi('live:liveAfteraSales:edit')")
    @Log(title = "售后记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LiveAfterSales liveAfterSales)
    {
        return toAjax(liveAfterSalesService.updateLiveAfterSales(liveAfterSales));
    }

    /**
     * 删除售后记录
     */
    @PreAuthorize("@ss.hasPermi('live:liveAfteraSales:remove')")
    @Log(title = "售后记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(liveAfterSalesService.deleteLiveAfterSalesByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('store:storeAfterSales:audit1')")
    @PostMapping("/audit1")
    //平台审核
    public R audit1(@RequestBody LiveAfterSalesAudit1Param param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setOperator(loginUser.getUser().getNickName());
        return liveAfterSalesService.audit1(param);
    }

    @PreAuthorize("@ss.hasPermi('store:storeAfterSales:audit2')")
    @PostMapping("/audit2")
    //仓库审核
    public R audit2(@RequestBody LiveAfterSalesAudit2Param param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setOperator(loginUser.getUser().getNickName());
        return liveAfterSalesService.audit2(param);
    }
    //财务审核
    @PreAuthorize("@ss.hasPermi('store:storeAfterSales:refund')")
    @PostMapping("/refund")
    public R refund(@RequestBody LiveAfterSalesRefundParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setOperator(loginUser.getUser().getNickName());
        return liveAfterSalesService.refundMoney(param);
    }
    //平台撤销
    @PreAuthorize("@ss.hasPermi('store:storeAfterSales:cancel')")
    @PostMapping("/cancel")
    public R cancel(@RequestBody LiveAfterSalesCancelParam param) throws ParseException {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setOperator(loginUser.getUser().getNickName());
        return liveAfterSalesService.cancel(param);
    }
}
