package cn.ysk.cashier.controller.shop;

import cn.ysk.cashier.dto.shop.TbShopPurveyorTransactQueryCriteria;
import cn.ysk.cashier.mybatis.entity.TbShopPurveyorTransactPay;
import cn.ysk.cashier.mybatis.service.TbShopPurveyorTransactPayService;
import cn.ysk.cashier.pojo.shop.TbShopPurveyorTransact;
import cn.ysk.cashier.service.shop.TbShopPurveyorTransactService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
* @website https://eladmin.vip
* @author lyf
* @date 2024-01-23
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "帐目往来管理")
@RequestMapping("/api/tbShopPurveyorTransact")
public class TbShopPurveyorTransactController {

    private final TbShopPurveyorTransactService tbShopPurveyorTransactService;

    private final TbShopPurveyorTransactPayService transactPayService;

    /**
     * 供应商列表
     * @param criteria
     * @return
     */
    @GetMapping
    @ApiOperation("供应商列表")
    public ResponseEntity<Object> queryTransactDate(TbShopPurveyorTransactQueryCriteria criteria){
        return new ResponseEntity<>(tbShopPurveyorTransactService.queryTransactDate(criteria),HttpStatus.OK);
    }

    /**
     * 进货账目详情
     * @param criteria
     * @return
     */
    @PostMapping("/info")
    public ResponseEntity<Object> queryPurveyorTransact(@RequestBody TbShopPurveyorTransactQueryCriteria criteria){
        return new ResponseEntity<>(tbShopPurveyorTransactService.queryPurveyorTransact(criteria),HttpStatus.OK);
    }

    /**
     * 汇总
     * @param criteria
     * @return
     */
    @GetMapping("/sum")
    public ResponseEntity<Object> queryTransactSum(TbShopPurveyorTransactQueryCriteria criteria){
        return new ResponseEntity<>(tbShopPurveyorTransactService.queryTransactSum(criteria),HttpStatus.OK);
    }

    @PostMapping("/payTransact")
    @ApiOperation("账单付款")
    public ResponseEntity<Object> payTransact(@RequestBody TbShopPurveyorTransact resources){
        tbShopPurveyorTransactService.payTransact(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/transactPayInfos")
    @ApiOperation("付款记录")
    public ResponseEntity<Object> transactPayInfos(@RequestParam Integer id,@RequestParam String type){
        QueryWrapper<TbShopPurveyorTransactPay> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("transact_id", id);
        if(StringUtils.isNotBlank(type)){
            queryWrapper.like("pay_type", type);
        }
        queryWrapper.orderByDesc("create_time");
        return new ResponseEntity<>(transactPayService.list(queryWrapper),HttpStatus.OK);
    }

    @PostMapping
    @ApiOperation("新增帐目往来")
    public ResponseEntity<Object> createTbShopPurveyorTransact(@Validated @RequestBody TbShopPurveyorTransact resources){
        return new ResponseEntity<>(tbShopPurveyorTransactService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @ApiOperation("修改帐目往来")
    public ResponseEntity<Object> updateTbShopPurveyorTransact(@Validated @RequestBody TbShopPurveyorTransact resources){
        tbShopPurveyorTransactService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @ApiOperation("删除帐目往来")
    public ResponseEntity<Object> deleteTbShopPurveyorTransact(@RequestBody Integer[] ids) {
        tbShopPurveyorTransactService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}