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

import com.bonus.canteen.core.account.constants.CardRecordTypeEnum;
import com.bonus.canteen.core.account.constants.CardStatusEnum;
import com.bonus.canteen.core.account.domain.AccountCard;
import com.bonus.canteen.core.account.service.IAccCardService;
import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 人员卡片资料Controller
 * 
 * @author xsheng
 * @date 2025-04-20
 */
@Api(tags = "人员卡片资料接口")
@RestController
@RequestMapping("/acc_card")
public class AccCardController extends BaseController {
    @Autowired
    private IAccCardService accCardService;

    /**
     * 查询人员卡片资料列表
     */
    @ApiOperation(value = "查询人员卡片资料列表")
    //@RequiresPermissions("account:card:list")
    @GetMapping("/list")
    public TableDataInfo list(AccountCard accountCard) {
        startPage();
        List<AccountCard> list = accCardService.selectAccCardList(accountCard);
        return getDataTable(list);
    }

    /**
     * 导出人员卡片资料列表
     */
    @ApiOperation(value = "导出人员卡片资料列表")
    //@PreventRepeatSubmit
    //@RequiresPermissions("account:card:export")
    @SysLog(title = "人员卡片资料", businessType = OperaType.EXPORT, logType = 1,module = "仓储管理->导出人员卡片资料")
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccountCard accountCard) {
        List<AccountCard> list = accCardService.selectAccCardList(accountCard);
        ExcelUtil<AccountCard> util = new ExcelUtil<AccountCard>(AccountCard.class);
        util.exportExcel(response, list, "人员卡片资料数据");
    }

    /**
     * 获取人员卡片资料详细信息
     */
    @ApiOperation(value = "获取人员卡片资料详细信息")
    //@RequiresPermissions("account:card:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(accCardService.selectAccCardById(id));
    }

    /**
     * 新增人员卡片资料
     */
    @ApiOperation(value = "新增人员卡片资料")
    //@PreventRepeatSubmit
    //@RequiresPermissions("account:card:add")
    @SysLog(title = "人员卡片资料", businessType = OperaType.INSERT, logType = 1,module = "仓储管理->新增人员卡片资料")
    @PostMapping
    public AjaxResult add(@RequestBody AccountCard accountCard) {
        try {
            accountCard.setOperationType(CardRecordTypeEnum.APPLY.getKey());
            return toAjax(accCardService.insertAccCard(accountCard));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改人员卡片资料
     */
    @ApiOperation(value = "修改人员卡片资料")
    //@PreventRepeatSubmit
    //@RequiresPermissions("account:card:edit")
    @SysLog(title = "人员卡片资料", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->修改人员卡片资料")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody AccountCard accountCard) {
        try {
            return toAjax(accCardService.updateAccCard(accountCard));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @ApiOperation(value = "换卡")
    //@PreventRepeatSubmit
    //@RequiresPermissions("account:card:edit")
    @SysLog(title = "人员卡片资料", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->换卡")
    @PostMapping("/change")
    public AjaxResult change(@RequestBody AccountCard accountCard) {
        try {
            accountCard.setOperationType(CardRecordTypeEnum.CHANGE.getKey());
            return toAjax(accCardService.updateAccCard(accountCard));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @ApiOperation(value = "挂失")
    //@PreventRepeatSubmit
    //@RequiresPermissions("account:card:edit")
    @SysLog(title = "人员卡片资料", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->挂失")
    @PostMapping("/loss")
    public AjaxResult loss(@RequestBody AccountCard accountCard) {
        try {
            //accCard.setOperationType(CardRecordTypeEnum.LOSS.getKey());
            accountCard.setCardStatus(CardStatusEnum.LOSS.getKey());
            return toAjax(accCardService.updateAccCard(accountCard));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @ApiOperation(value = "解挂")
    //@PreventRepeatSubmit
    //@RequiresPermissions("account:card:edit")
    @SysLog(title = "人员卡片资料", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->解挂")
    @PostMapping("/relieve")
    public AjaxResult relieve(@RequestBody AccountCard accountCard) {
        try {
            //accCard.setOperationType(CardRecordTypeEnum.RELIEVE.getKey());
            accountCard.setCardStatus(CardStatusEnum.NORMAL.getKey());
            return toAjax(accCardService.updateAccCard(accountCard));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @ApiOperation(value = "退卡")
    //@PreventRepeatSubmit
    //@RequiresPermissions("account:card:edit")
    @SysLog(title = "人员卡片资料", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->退卡")
    @PostMapping("/refund")
    public AjaxResult refund(@RequestBody AccountCard accountCard) {
        try {
            accountCard.setOperationType(CardRecordTypeEnum.REFUND.getKey());
            accountCard.setCardStatus(CardStatusEnum.REFUND.getKey());
            return toAjax(accCardService.updateAccCard(accountCard));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

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