/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.distribution.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.comment.dto.DistributionAuditingDto;
import com.yami.shop.distribution.comment.model.DistributionAuditing;
import com.yami.shop.distribution.comment.model.DistributionUser;
import com.yami.shop.distribution.comment.param.RangeTimeParam;
import com.yami.shop.distribution.comment.service.DistributionAuditingService;
import com.yami.shop.distribution.comment.service.DistributionUserService;
import com.yami.shop.security.platform.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 * @author lgh on 2019/04/01.
 */
@RestController
@RequestMapping("/distribution/distributionAuditing")
@AllArgsConstructor
public class DistributionAuditingController {

    private final DistributionAuditingService distributionAuditingService;

    private final DistributionUserService distributionUserService;


    /**
     * 分页获取
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('distribution:distributionAuditing:page')")
    public ResponseEntity<IPage<DistributionAuditingDto>> page(DistributionAuditing distributionAuditing, RangeTimeParam rangeTimeParam,
                                                               String mobile, Integer startExpenseNumber, Integer endExpenseNumber,
                                                               Double startPayAmount, Double endPayAmount, PageParam<DistributionAuditing> page) {
        IPage<DistributionAuditingDto> list = distributionAuditingService.distributionAuditingPage(page, distributionAuditing, rangeTimeParam, startExpenseNumber, endExpenseNumber, startPayAmount, endPayAmount, mobile);
        return ResponseEntity.ok(list);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@pms.hasPermission('distribution:distributionAuditing:info')")
    public ResponseEntity<DistributionAuditing> info(@PathVariable("id") Long id) {
        DistributionAuditing distributionAuditing = distributionAuditingService.getById(id);
        return ResponseEntity.ok(distributionAuditing);
    }

    /**
     * 审核
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('distribution:distributionAuditing:update')")
    public ResponseEntity<Void> update(@RequestBody @Valid DistributionAuditing distributionAuditing) {
        distributionAuditing.setUpdateTime(new Date());
        distributionAuditing.setModifier(SecurityUtils.getSysUser().getUserId());
        distributionAuditingService.examine(distributionAuditing);
        DistributionUser distributionUser = distributionUserService.getById(distributionAuditing.getDistributionUserId());
        distributionUserService.removeCacheByUserIdAndShopId(distributionUser.getUserId(), distributionAuditing.getShopId());
        distributionUserService.removeCacheByCardNo(distributionUser.getCardNo());
        return ResponseEntity.ok().build();
    }

//	/**
//	 * 保存
//	 */
//	@PostMapping
//	@PreAuthorize("@pms.hasPermission('distribution:distributionAuditing:save')")
//	public ResponseEntity<Void> save(@RequestBody @Valid DistributionAuditing distributionAuditing){
//		distributionAuditingService.save(distributionAuditing);
//		return ResponseEntity.ok().build();
//	}

//	/**
//	 * 删除
//	 */
//	@DeleteMapping("/{id}")
//	@PreAuthorize("@pms.hasPermission('distribution:distributionAuditing:delete')")
//	public ResponseEntity<Void> delete(@PathVariable Long id){
//		distributionAuditingService.removeById(id);
//		return ResponseEntity.ok().build();
//	}
}
