package com.hzs.ground.margin.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.ScmMsgConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.ground.margin.ThMargin;
import com.hzs.common.security.service.UserTokenService;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.ground.margin.service.IThMarginService;
import com.hzs.ground.margin.vo.ThMarginVO;
import com.hzs.ground.product.service.IThApprovalLogService;
import com.hzs.ground.supplier.service.IThSupplierApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 保证金缴纳 前端控制器
 *
 * @author hzs
 * @since 2024-03-21
 */
@RestController
@RequestMapping("/manage/th-margin")
public class ThMarginController extends BaseController {

    @Autowired
    private IThMarginService iThMarginService;
    @Autowired
    private IThSupplierApplyService iThSupplierApplyService;

    /**
     * @description: 保证金缴纳列表
     * @author: zhang jing
     * @date: 2024/3/22 11:35
     * @param: [thMarginVO]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @GetMapping("/list")
    @Log(module = EOperationModule.TH_SUPPLIER_APPLY, business = EOperationBusiness.TH_SUPPLIER_APPLY, method = EOperationMethod.SELECT)
    public TableDataInfo listGeneralIn(ThMarginVO thMarginVO) {
        Long userId = SecurityUtils.getUserId();
        thMarginVO.setPkCountry(SecurityUtils.getPkCountry());
        Integer checkUser = iThSupplierApplyService.queryThSupplierApplyUserList(userId);
        if (checkUser > 0) {
            thMarginVO.setPkCreator(userId);
        }
        startPage();
        List<ThMarginVO> thSupplierApplyList = iThMarginService.queryThMarginList(thMarginVO);
        return getDataTable(thSupplierApplyList);
    }

    /**
     * @description: 添加保证金缴纳
     * @author: zhang jing
     * @date: 2024/3/22 11:53
     * @param: [thMarginVO]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/save")
    public AjaxResult save(@RequestBody ThMarginVO thMarginVO) {
        int pkCountry = SecurityUtils.getPkCountry();
        // 验证必填信息
        if (StringUtils.isNull(thMarginVO.getMargin())) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.TH_MARGIN_MARGIN));
        }
        thMarginVO.setPkCountry(pkCountry);
        thMarginVO.setPkCreator(SecurityUtils.getUserId());
        thMarginVO.setCreationTime(new Date());
        QueryWrapper<ThMargin> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_SUPPLIER", thMarginVO.getPkSupplier());
        List thList = iThMarginService.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(thList)) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.TH_MARGIN_REPEAT));
        }
        iThMarginService.saveThMargin(thMarginVO);
        return AjaxResult.success();
    }

    /**
     * @description: 修改保证金缴纳
     * @author: zhang jing
     * @date: 2024/3/22 11:55
     * @param: [thMarginVO]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/update")
    public AjaxResult update(@RequestBody ThMarginVO thMarginVO) {
        int pkCountry = SecurityUtils.getPkCountry();
        // 验证必填信息
        if (StringUtils.isNull(thMarginVO.getMargin())) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.TH_MARGIN_MARGIN));
        }
        QueryWrapper<ThMargin> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_SUPPLIER", thMarginVO.getPkSupplier());
        queryWrapper.ne("PK_ID", thMarginVO.getPkId());
        List thList = iThMarginService.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(thList)) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.TH_MARGIN_REPEAT));
        }
        thMarginVO.setPkCountry(pkCountry);
        thMarginVO.setPkCreator(SecurityUtils.getUserId());
        thMarginVO.setCreationTime(new Date());
        iThMarginService.updateThMargin(thMarginVO);
        return AjaxResult.success();
    }

    /**
     * @description: 查询保证金缴纳
     * @author: zhang jing
     * @date: 2024/3/22 13:50
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        ThMargin tsa = iThMarginService.getById(pkId);
        return AjaxResult.success(tsa);
    }

    /**
     * @description: 删除保证金缴纳
     * @author: zhang jing
     * @date: 2024/3/22 13:49
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        return AjaxResult.success(iThMarginService.delThMargin(pkId));
    }


}
