package com.bonus.canteen.core.account.controller;

import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.bonus.canteen.core.account.constants.AccTradeTypeEnum;
import com.bonus.canteen.core.account.domain.AccInfo;
import com.bonus.canteen.core.account.domain.param.*;
import com.bonus.canteen.core.account.domain.vo.AccInfoDetailsVO;

import com.bonus.canteen.core.account.service.AccOperationHistoryService;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.log.enums.OperaType;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.system.api.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.canteen.core.account.service.IAccInfoService;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.page.TableDataInfo;

import static com.bonus.common.core.web.domain.AjaxResult.success;

/**
 * 账户资料Controller
 * 
 * @author xsheng
 * @date 2025-04-05
 */
@Api(tags = "账户资料接口")
@RestController
@RequestMapping("/acc_info")
public class AccInfoController extends BaseController {
    @Autowired
    private IAccInfoService accInfoService;
    @Autowired
    private AccOperationHistoryService accOperationHistoryService;

    /**
     * 查询账户资料列表
     */
    @ApiOperation(value = "查询账户资料列表")
    //@RequiresPermissions("account:info:list")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody AccountInfoQueryParam accountInfoQueryParam) {
        startPage();
        return getDataTable(accInfoService.selectAccInfoList(accountInfoQueryParam));
    }

    /**
     * 导出账户资料列表
     */
    @ApiOperation(value = "导出账户资料列表")
    //@PreventRepeatSubmit
    //@RequiresPermissions("account:info:export")
    @SysLog(title = "账户资料", businessType = OperaType.EXPORT, logType = 1,module = "仓储管理->导出账户资料")
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccountInfoQueryParam accountInfoQueryParam) {
        List<AccInfoDetailsVO> list = accInfoService.selectAccInfoList(accountInfoQueryParam);
        ExcelUtil<AccInfoDetailsVO> util = new ExcelUtil<>(AccInfoDetailsVO.class);
        util.exportExcel(response, list, "账户资料数据");
    }

    /**
     * 获取账户资料详细信息
     */
    @ApiOperation(value = "获取账户资料详细信息")
    //@RequiresPermissions("account:info:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(accInfoService.selectAccInfoById(id));
    }

    /**
     * 新增账户资料
     */
    @ApiOperation(value = "新增账户资料")
    //@PreventRepeatSubmit
    //@RequiresPermissions("account:info:add")
    @SysLog(title = "账户资料", businessType = OperaType.INSERT, logType = 1,module = "仓储管理->新增账户资料")
    @PostMapping
    public AjaxResult add(@RequestBody AccInfo accInfo) {
        try {
            return toAjax(accInfoService.insertAccInfo(accInfo));
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 新增账户资料
     */
    @ApiOperation(value = "新增账户资料")
    //@PreventRepeatSubmit
    //@RequiresPermissions("account:info:add")
    @SysLog(title = "账户资料", businessType = OperaType.INSERT, logType = 1,module = "仓储管理->新增账户资料")
    @PostMapping("sync")
    public AjaxResult syncAccInfo(@RequestBody SysUser sysUser) {
        try {
            return toAjax(accInfoService.syncAccInfo(sysUser));
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 修改账户资料
     */
//    @ApiOperation(value = "修改账户余额以及有效期")
//    //@PreventRepeatSubmit
//    //@RequiresPermissions("account:info:edit")
//    @SysLog(title = "账户资料", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->修改账户资料")
//    @PostMapping("/balance/edit")
//    public AjaxResult edit(@RequestBody AccountBalanceEditParam accountBalanceEditParam) {
//        this.accInfoService.updateAccInfo(accountBalanceEditParam);
//        return AjaxResult.success();
//    }

    /**
     * 删除账户资料
     */
    @ApiOperation(value = "删除账户资料")
    //@PreventRepeatSubmit
    //@RequiresPermissions("account:info:remove")
    @SysLog(title = "账户资料", businessType = OperaType.DELETE, logType = 1,module = "仓储管理->删除账户资料")
	@PostMapping("/del/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(accInfoService.deleteAccInfoByIds(ids));
    }

//    @ApiOperation("账户注销/过期数量")
//    @PostMapping({"/invalid/sum"})
//    public AjaxResult queryAccInfoExpiredCount() {
//        return AjaxResult.success(this.accInfoService.queryAccInfoInvalidSum());
//    }
    @ApiOperation("查询账户余额统计")
    @PostMapping({"/balance/sum"})
    public AjaxResult queryAccInfoBalanceSum(@RequestBody AccountInfoQueryParam accountInfoQueryParam) {
        return AjaxResult.success(this.accInfoService.queryAccInfoBalanceSum(accountInfoQueryParam));
    }

    @ApiOperation("账户启用、停用")
    @PostMapping({"/status/edit"})
    public AjaxResult editAccountStatus(@RequestBody @Valid AccountEnableDisableParam accountEnableDisableParam) {
        this.accInfoService.editAccountStatus(accountEnableDisableParam);
        return AjaxResult.success();
    }

    @ApiOperation("账户操作记录")
    @PostMapping({"/operation/record"})
    public TableDataInfo queryPageAccOperationRecord(@RequestBody AccOperationQueryParam param) {
        startPage();
        return getDataTable(this.accOperationHistoryService.selectAccOperationHistory(param));
    }

    /**
     * 删除账户资料
     */
    @ApiOperation(value = "删除账户资料")
    //@PreventRepeatSubmit
    //@RequiresPermissions("account:info:remove")
    @SysLog(title = "账户资料", businessType = OperaType.DELETE, logType = 1,module = "账户管理->删除账户资料")
    @PostMapping("/deleteAccInfoByUserIds")
    public AjaxResult deleteAccInfoByUserIds(@RequestBody List<SysUser> users) {
        return toAjax(accInfoService.deleteAccInfoByUserIds(users));
    }

    @ApiOperation(value = "获取支付二维码")
    @PostMapping(value = "/getOrderQRCode")
    public AjaxResult getOrderQRCode() {
        return success(accInfoService.getOrderQRCode());
    }

    @ApiOperation(
            value = "查询账户(仅返回钱包余额)",
            notes = "移动端查询账户(仅返回钱包余额)"
    )
    @PostMapping({"/wallet/balance"})
    public AjaxResult queryWalletBalance(@RequestBody AccInfo accInfo) {
        return success(this.accInfoService.queryWalletBalance(accInfo));
    }
    @ApiOperation("单人充值")
    @PostMapping("/recharge")
    public AjaxResult individualAccRechargeA(@RequestBody @Valid AccRechargeParam param) {
        this.accInfoService.individualAccRecharge(param);
        return AjaxResult.success();
    }

    @ApiOperation("单人取钱")
    @PostMapping("/withdraw")
    public AjaxResult individualAccWithdraw(@RequestBody @Valid AccWithdrawParam param) {
        this.accInfoService.individualAccWithdraw(param);
        return AjaxResult.success();
    }

    @ApiOperation("离职清空余额(个人钱包和补贴钱包)")
    @PostMapping({"/leave/clear"})
    public AjaxResult individualAccLeaveClear(@RequestBody @Valid AccLeaveClearParam param) {
        this.accInfoService.individualAccLeaveClear(param);
        return AjaxResult.success();
    }

    @ApiOperation("个人钱包操作记录")
    @PostMapping({"/wallet/history"})
    public TableDataInfo queryAccPersonalWalOperationAddList(@RequestBody AccWalletOperationQueryParam request) {
        if(Objects.isNull(request)) {
            throw new ServiceException("请求参数为空");
        }
        if(Objects.isNull(request.getTradeType())) {
            throw new ServiceException("交易类型为空");
        }
        startPage();
        return getDataTable(accInfoService.queryAccPersonalWalOperationAddList(request));
    }

    @ApiOperation("手机端个人钱包操作记录")
    @PostMapping({"/mobile/wallet/history"})
    public TableDataInfo queryAccPersonalWalOperationAddListForMobile(@RequestBody AccWalletOperationQueryParam request) {
        if(Objects.isNull(request)) {
            throw new ServiceException("请求参数为空");
        }
        if(Objects.isNull(request.getTradeType())) {
            throw new ServiceException("交易类型为空");
        }
        startPage();
        request.setUserId(SecurityUtils.getUserId());
        return getDataTable(accInfoService.queryAccPersonalWalOperationAddList(request));
    }
}
