package org.dromara.work.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.dto.RoleDTO;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.work.domain.bo.TpReceiptBo;
import org.dromara.work.domain.vo.ReceiptSumVo;
import org.dromara.work.domain.vo.TpReceiptVo;
import org.dromara.work.service.ITpReceiptService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 收款管理
 *
 * @author Maosw
 * @date 2024-08-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/receipt")
public class TpReceiptController extends BaseController {

    private final ITpReceiptService tpReceiptService;

    /**
     * 查询收款管理列表
     */
    @SaCheckPermission("work:receipt:list")
    @GetMapping("/list")
    public TableDataInfo<TpReceiptVo> list(TpReceiptBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(loginUser.getIdentity() == 2){
            throw new ServiceException("您无权限查看汇款信息");
        }
        return tpReceiptService.queryPageList(bo, pageQuery);
    }


    /**
     * 查询收款统计信息
     */
    @SaCheckPermission("work:receiptSum:list")
    @GetMapping("/receiptSum")
    public R<ReceiptSumVo> receiptSum(TpReceiptBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(loginUser.getIdentity() == 2){
            throw new ServiceException("您无权限查看汇款信息");
        }
        return R.ok(tpReceiptService.queryListSum(bo));
    }


    /**
     * 导出收款管理列表
     */
    @SaCheckPermission("work:receipt:export")
    @Log(title = "收款管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpReceiptBo bo, HttpServletResponse response) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(loginUser.getIdentity() == 2){
            throw new ServiceException("您无权限查看汇款信息");
        }
        List<TpReceiptVo> list = tpReceiptService.queryList(bo);
        ExcelUtil.exportExcel(list, "收款管理", TpReceiptVo.class, response);
    }

    /**
     * 获取收款管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:receipt:query")
    @GetMapping("/{id}")
    public R<TpReceiptVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpReceiptService.queryById(id));
    }

    /**
     * 新增收款管理
     */
    @SaCheckPermission("work:receipt:add")
    @Log(title = "收款管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpReceiptBo bo) {
        return toAjax(tpReceiptService.insertByBo(bo));
    }

    /**
     * 修改收款管理
     */
    /*@SaCheckPermission("work:receipt:edit")
    @Log(title = "收款管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpReceiptBo bo) {
        return toAjax(tpReceiptService.updateByBo(bo));
    }*/

    /**
     * 认领汇款
     */
    @SaCheckPermission("work:receipt:claim")
    @Log(title = "认领汇款", businessType = BusinessType.UPDATE)
    @PostMapping("/claim")
    @RepeatSubmit(interval = 5,timeUnit = TimeUnit.SECONDS,message = "不允许重复提交")
    public R<Void> claim(@RequestBody TpReceiptBo bo) {
        if(ObjectUtil.isNull(bo.getCid())){
            return R.fail("客户ID不能为空");
        }

        LoginUser loginUser = LoginHelper.getLoginUser();
        List<RoleDTO> list1 = loginUser.getRoles();
        boolean isRole = list1.stream().anyMatch(role -> role.getRoleId() == 1 || role.getRoleId() == 2);
        if(!isRole){
            if(loginUser.getIdentity() == 2){
                throw new ServiceException("您无认领汇款权限");
            }
        }
        return toAjax(tpReceiptService.updateClaimByBo(bo));
    }


    /**
     * 回退汇款
     */
    @SaCheckPermission("work:receipt:back")
    @Log(title = "回退汇款", businessType = BusinessType.UPDATE)
    @PostMapping("/back")
    @RepeatSubmit(interval = 5,timeUnit = TimeUnit.SECONDS,message = "不允许重复提交")
    public R<Void> back(@RequestBody TpReceiptBo bo) {
        /*LoginUser loginUser = LoginHelper.getLoginUser();
        List<RoleDTO> list1 = loginUser.getRoles();
        boolean isRole = list1.stream().anyMatch(role -> role.getRoleId() == 1 || role.getRoleId() == 2);
        if(!isRole){
            if(loginUser.getIdentity() == 2 || loginUser.getIdentity() == 3 || loginUser.getIdentity() == 4){
                throw new ServiceException("您无回退汇款权限");
            }
        }*/
        return toAjax(tpReceiptService.updateBackByBo(bo));
    }

    /**
     * 作废汇款
     *
     * @param ids 主键串
     */
    @SaCheckPermission("work:receipt:remove")
    @Log(title = "作废汇款", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tpReceiptService.deleteWithValidByIds(List.of(ids), true));
    }
}
