package com.red.circle.console.adapter.app.user;


import com.alibaba.excel.EasyExcel;
import com.red.circle.console.app.dto.clienobject.user.UserFreightBalanceCO;
import com.red.circle.console.app.dto.clienobject.user.UserFreightBalanceRunningWaterCO;
import com.red.circle.console.app.excel.model.freight.UserFreightBalanceRunningWaterExportModel;
import com.red.circle.console.app.service.app.user.UserFreightService;
import com.red.circle.console.infra.annotations.OpsOperationReqLog;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.other.inner.model.cmd.user.UserFreightBalanceCmd;
import com.red.circle.wallet.inner.model.cmd.UpdateFreightBalanceInfoCmd;
import com.red.circle.wallet.inner.model.cmd.UserFreightBalancePageQryCmd;
import com.red.circle.wallet.inner.model.cmd.UserFreightBalanceRunningWaterExportQryCmd;
import com.red.circle.wallet.inner.model.cmd.UserFreightBalanceRunningWaterPageQryCmd;
import com.red.circle.wallet.inner.model.cmd.UserFreightSellerCmd;
import com.red.circle.wallet.inner.model.cmd.UserFreightSellerRunningWaterPageQryCmd;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户货运账户 前端控制器
 * </p>
 *
 * @author pengliang
 * @since 2021-07-07
 */
@RestController
@RequestMapping("/freight")
@RequiredArgsConstructor
public class UserFreightRestController extends BaseController {


  private final UserFreightService userFreightService;

  /**
   * 获取代理账户信息-分页列表.
   */
  @GetMapping("/page")
  public PageResult<UserFreightBalanceCO> pageFreight(UserFreightBalancePageQryCmd query) {
    return userFreightService.pageFreight(query);
  }

  /**
   * 发货给代理账户.
   */
  @OpsOperationReqLog("货运代理-发货")
  @PostMapping("/ship")
  public void ship(@RequestBody @Validated UserFreightBalanceCmd param) {
    param.setOperationUser(param.requiredReqUserId());
    userFreightService.ship(param);
  }

  /**
   * 切换关闭账号状态.
   */
  @OpsOperationReqLog("货运代理-关闭账号状态")
  @PostMapping("/switch-status/{id}/{status}")
  public void switchStatus(@PathVariable("id") Long id,
      @PathVariable("status") Boolean status) {
    userFreightService.switchStatus(id, status);
  }

  /**
   * 切换是否在H5显示货运代理
   */
  @OpsOperationReqLog("货运代理-切换是否在H5显示")
  @PostMapping("/show-status/{id}/{status}")
  public void showStatus(@PathVariable("id") Long id,
      @PathVariable("status") Boolean status) {
    userFreightService.showStatus(id, status);
  }

  /**
   * 是否经销商切换
   */
  @OpsOperationReqLog("货运代理-是否经销商开关")
  @PostMapping("/switch-dealer-status/{id}/{status}")
  public void switchDealerStatus(@PathVariable("id") Long id,
      @PathVariable("status") Boolean status) {
    userFreightService.switchDealerStatus(id, status);
  }

  /**
   * 是否超级经销商切换
   */
  @OpsOperationReqLog("货运代理-是否超级经销商开关")
  @PostMapping("/switch-superdealer-status/{id}/{status}")
  public void switchSuperDealerStatus(@PathVariable("id") Long id,
                                 @PathVariable("status") Boolean status) {
    userFreightService.switchSuperDealerStatus(id, status);
  }

  /**
   * 扣除货款.
   */
  @PostMapping("/deduction")
  public void deduction(@RequestBody @Validated UserFreightBalanceCmd param) {
    param.setOperationUser(param.getReqUserId());
    userFreightService.deduction(param);
  }

  /**
   * 流水.
   */
  @GetMapping("/running-water-page")
  public PageResult<UserFreightBalanceRunningWaterCO> pageRunningWater(
      UserFreightBalanceRunningWaterPageQryCmd query) {
    return userFreightService.pageRunningWater(query);
  }

  /**
   * 获取指定卖家货运流水.
   */
  @GetMapping("/freight-seller-running-water-page")
  public PageResult<UserFreightBalanceRunningWaterCO> pageFreightSellerRunningWater(
      UserFreightSellerRunningWaterPageQryCmd query) {
    return userFreightService.pageFreightSellerRunningWater(query);
  }


  /**
   * 流水-导出.
   */
  @OpsOperationReqLog(value = "货运代理-流水导出", ignoreRequestParam = true)
  @GetMapping("/running-water/export")
  public void exportRunningWater(HttpServletResponse response,
      @Validated UserFreightBalanceRunningWaterExportQryCmd query) throws IOException {

    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setCharacterEncoding("utf-8");
    String fileName = URLEncoder.encode("Export Freight RunningWater " + query.getMonthDate(),
            StandardCharsets.UTF_8)
        .replaceAll("\\+", "%20");
    String sheetName = URLEncoder.encode("Export Freight RunningWater " + query.getMonthDate(),
            StandardCharsets.UTF_8)
        .replaceAll("\\+", "%20");
    response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
    EasyExcel.write(response.getOutputStream(), UserFreightBalanceRunningWaterExportModel.class)
        .sheet(sheetName).doWrite(userFreightService.listExport(query));
  }

  /**
   * 修改经销商卖家数量
   */
  @PostMapping("/update-seller-quantity")
  public void updateSellerQuantity(@RequestBody @Validated UserFreightSellerCmd param) {
    param.setOperationUser(getReqUserId());
    userFreightService.updateSellerQuantity(param);
  }

  /**
   * 更新货运代理账户信息（国家和联系方式）.
   */
  @OpsOperationReqLog("货运代理-更新账户信息")
  @PostMapping("/update-info")
  public void updateFreightBalanceInfo(@RequestBody @Validated UpdateFreightBalanceInfoCmd cmd) {
    userFreightService.updateFreightBalanceInfo(cmd);
  }

}
