package com.cpop.oam.business.controller.backstage;

import com.cpop.common.utils.bean.BeanUtils;
import com.cpop.core.base.R;
import com.cpop.oam.business.bo.*;
import com.cpop.oam.business.entity.FinanceReimburseType;
import com.cpop.oam.business.service.FinanceReimburseService;
import com.cpop.oam.business.service.FinanceReimburseStageService;
import com.cpop.oam.business.service.FinanceReimburseTypeService;
import com.cpop.oam.business.vo.*;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.cpop.oam.business.entity.table.FinanceReimburseStageTableDef.FINANCE_REIMBURSE_STAGE;
import static com.cpop.oam.business.entity.table.FinanceReimburseTableDef.FINANCE_REIMBURSE;

/**
 * 报销记录表 控制层。
 *
 * @author DB
 * @since 2023-09-20
 */
@RestController
@Api(tags = "财务报销模块")
@RequestMapping("/backstage/financeReimburse")
public class BackstageFinanceReimburseController {

    @Autowired
    private FinanceReimburseService financeReimburseService;

    @Autowired
    private FinanceReimburseTypeService financeReimburseTypeService;

    @Autowired
    private FinanceReimburseStageService financeReimburseStageService;

    /**
     * @Description: 财务报销模块-报销分页列表
     * @param bo 请求参数
     * @return: R<Page<FinanceReimbursePageVo>>
     * @Author: DB
     * @Date: 2023/5/30 15:44
     **/
    @PreAuthorize("@aps.hasPermission('financial:application:list')")
    @ApiOperation("财务报销模块-报销申请分页列表")
    @GetMapping("/getFinanceReimbursePage")
    public R<Page<FinanceReimbursePageVo>> getFinanceReimbursePage(FinanceReimbursePageBo bo) {
        Page<FinanceReimbursePageVo> pageVo = financeReimburseService.getFinanceReimbursePage(bo);
        return R.ok(pageVo);
    }

    /**
     * @Description: 财务报销模块-报销类型列表
     * @return: R<Page<FinanceReimbursePageVo>>
     * @Author: DB
     * @Date: 2023/5/30 15:44
     **/
    @ApiOperation("财务报销模块-报销类型列表")
    @GetMapping("/getFinanceReimburseTypeList")
    public R<List<FinanceReimburseTypeListVo>> getFinanceReimburseTypeList() {
        List<FinanceReimburseTypeListVo> list = financeReimburseTypeService.listAs(QueryWrapper.create(), FinanceReimburseTypeListVo.class);
        return R.ok(list);
    }

    /**
     * @descriptions 新增报销申请
     * @author DB
     * @date 2023/09/21 15:55
     * @param bo 请求参数
     * @return com.pupu.core.base.R<java.lang.Void>
     */
    @ApiOperation("新增报销申请")
    @PostMapping("/insertReimburseApplication")
    public R<Void> insertReimburseApplication(@RequestBody @Validated ReimburseApplicationBo bo) {
        financeReimburseService.insertReimburseApplication(bo);
        return R.ok();
    }

    /**
     * 修改报销申请
     * @author DB
     * @since 2023/12/21
     * @param bo 请求
     * @return R<Void>
     */
    @ApiOperation("修改报销申请")
    @PutMapping("/updateReimburseApplication")
    public R<Void> updateReimburseApplication(@RequestBody @Validated ReimburseApplicationBo bo) {
        financeReimburseService.updateReimburseApplication(bo);
        return R.ok();
    }

    /**
     * 财务报销模块-报销审核分页列表
     * @param bo 请求参数
     * @return R<Page<FinanceReimbursePageVo>>
     * @author DB
     * @since : 2023/5/30 15:44
     **/
    @PreAuthorize("@aps.hasPermission('financial:audit:list')")
    @ApiOperation("财务报销模块-报销审核分页列表")
    @GetMapping("/getReimburseAuditPage")
    public R<Page<FinanceReimburseAuditPageVo>> getReimburseAuditPage(FinanceReimburseAuditPageBo bo) {
        Page<FinanceReimburseAuditPageVo> pageVo = financeReimburseService.getReimburseAuditPage(bo);
        return R.ok(pageVo);
    }

    /**
     * @Description: 修改报销状态
     * @param bo 请求参数
     * @Author: DB
     * @Date: 2023/5/10 16:01
     **/
    @ApiOperation("修改报销状态")
    @PutMapping("/updateReimburseStatus")
    public R<Void> updateReimburseStatus(@RequestBody @Validated ReimburseStatusBo bo) {
        financeReimburseService.updateReimburseStatus(bo);
        return R.ok();
    }

    /**
     * @descriptions 新增报销类型
     * @author DB
     * @date 2023/09/21 15:55
     * @param bo 请求参数
     * @return com.pupu.core.base.R<java.lang.Void>
     */
    @ApiOperation("新增报销类型")
    @PostMapping("/insertReimburseType")
    public R<Void> insertReimburseApplication(@RequestBody @Validated ReimburseTypeBo bo) {
        financeReimburseTypeService.save(BeanUtils.mapToClass(bo, FinanceReimburseType.class));
        return R.ok();
    }

    /**
     * @descriptions 修改报销类型
     * @author DB
     * @date 2023/09/08 15:20
     * @param bo 请求参数
     * @return com.jambox.core.base.R<java.lang.Void>
     */
    @ApiOperation("修改报销类型")
    @PutMapping("/updateReimburseType")
    public R<Void> updateReimburseType(@RequestBody @Validated ReimburseTypeBo bo) {
        financeReimburseTypeService.updateById(BeanUtils.mapToClass(bo, FinanceReimburseType.class));
        return R.ok();
    }

    /**
     * @descriptions 删除报销类型
     * @author DB
     * @date 2023/09/12 16:57
     * @param id 主键
     * @return com.jambox.core.base.R<java.lang.Void>
     */
    @ApiOperation("删除报销类型")
    @DeleteMapping("/removeReimburseTypeById/{id}")
    public R<Void> removeReimburseTypeById(@PathVariable String id) {
        financeReimburseTypeService.removeById(id);
        return R.ok();
    }

    /**
     * @Description: 财务报销模块-个人报销统计
     * @return: R<Page<FinanceReimbursePageVo>>
     * @Author: DB
     * @Date: 2023/5/30 15:44
     **/
    @ApiOperation("财务报销模块-个人报销统计")
    @GetMapping("/getPersonStatistic")
    public R<ReimbursePersonStatisticVo> getPersonStatistic() {
        return R.ok(financeReimburseService.getPersonStatistic());
    }

    /**
     * @Description: 审核员工申请报销
     * @Author: DB
     * @Date: 2023/5/10 16:01
     **/
    @ApiOperation("审核员工申请报销")
    @PutMapping("/auditReimburseApplication/{id}")
    public R<Void> auditReimburseApplication(@PathVariable String id) {
        financeReimburseService.updateChain().set(FINANCE_REIMBURSE.STATUS, 1).where(FINANCE_REIMBURSE.ID.eq(id)).update();
        return R.ok();
    }

    /**
     * @descriptions 报销驳回
     * @author DB
     * @date 2023/09/08 15:20
     * @param bo 请求参数
     * @return com.jambox.core.base.R<java.lang.Void>
     */
    @ApiOperation("报销驳回")
    @PutMapping("/reimburseReject")
    public R<Void> reimburseReject(@RequestBody @Validated ReimburseRejectBo bo) {
        financeReimburseService.updateChain()
                .set(FINANCE_REIMBURSE.STATUS, -1)
                .set(FINANCE_REIMBURSE.REJECT_REASON, bo.getRejectReason())
                .where(FINANCE_REIMBURSE.ID.eq(bo.getId()))
                .update();
        return R.ok();
    }

    /**
     * @descriptions 报销下款
     * @author DB
     * @date 2023/09/08 15:20
     * @param bo 请求参数
     * @return com.jambox.core.base.R<java.lang.Void>
     */
    @ApiOperation("报销下款")
    @PutMapping("/reimbursePay")
    public R<Void> reimbursePay(@RequestBody @Validated ReimbursePayBo bo) {
        financeReimburseService.reimbursePay(bo);
        return R.ok();
    }

    /**
     * @Description: 财务报销模块-报销分页列表
     * @param id 报销主键
     * @return: R<Page<FinanceReimbursePageVo>>
     * @Author: DB
     * @Date: 2023/5/30 15:44
     **/
    @ApiOperation("财务报销模块-报销详情记录列表")
    @GetMapping("/getReimburseRecordList/{id}")
    public R<List<FinanceReimburseRecordListVo>> getReimburseRecordList(@PathVariable String id) {
        List<FinanceReimburseRecordListVo> list = financeReimburseStageService.listAs(QueryWrapper.create()
                        .where(FINANCE_REIMBURSE_STAGE.FINANCE_REIMBURSE_ID.eq(id))
                , FinanceReimburseRecordListVo.class);
        return R.ok(list);
    }
}
