/*
 * 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.config.Constant;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.comment.dto.DistributionUserAchievementDataDto;
import com.yami.shop.distribution.comment.model.DistributionUser;
import com.yami.shop.distribution.comment.param.RangeTimeParam;
import com.yami.shop.distribution.comment.param.UpdateDistributionUserParam;
import com.yami.shop.distribution.comment.service.DistributionUserService;
import com.yami.shop.security.platform.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.List;

/**
 * 分销用户信息管理实现
 *
 * @author lgh
 * @version 1.0.0
 * @since 2019-04-01
 */
@RestController
@RequestMapping("/distribution/distributionUser")
public class DistributionUserController {

    @Autowired
    private DistributionUserService distributionUserService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('distribution:distributionUser:page')")
    public ResponseEntity<IPage<DistributionUser>> page(DistributionUser distributionUser, RangeTimeParam rangeTimeParam, String userMobile, String parentUserMobile, PageParam<DistributionUser> page) {
        IPage<DistributionUser> list = distributionUserService.distributionUserPage(page, distributionUser, rangeTimeParam, userMobile, parentUserMobile);
        return ResponseEntity.ok(list);
    }


    /**
     * 分销员各项数据信息
     */
    @GetMapping("/info/achievementData/{id}")
    @PreAuthorize("@pms.hasPermission('distribution:distributionUser:info')")
    public ResponseEntity<DistributionUserAchievementDataDto> getDistributionUserAchievementData(@PathVariable("id") Long id) {
        return ResponseEntity.ok(distributionUserService.getDistributionUserLevelAchievementDataByDistributionUserId(id));
    }


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


    /**
     * 获取业绩
     */
    @GetMapping("/page/achievement")
    @PreAuthorize("@pms.hasPermission('distribution:distributionUser:page')")
    public ResponseEntity<IPage<DistributionUser>> page(DistributionUser distributionUser, String userMobile, PageParam<DistributionUser> page) {
        distributionUser.setShopId(Constant.PLATFORM_SHOP_ID);
        IPage<DistributionUser> list = distributionUserService.getDistributionUserAchievementPage(page, distributionUser, userMobile);
        return ResponseEntity.ok(list);
    }


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

    /**
     * 修改
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('distribution:distributionUser:update')")
    public ResponseEntity<Void> update(@RequestBody @Valid DistributionUser distributionUser) {
        distributionUserService.updateById(distributionUser);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改分销员状态
     */
    @PutMapping("/state")
    @PreAuthorize("@pms.hasPermission('distribution:distributionUser:update')")
    public ResponseEntity<Void> update(@RequestBody UpdateDistributionUserParam param) {

        distributionUserService.updateSelectiveAndInsertDistributionUserBan(param, SecurityUtils.getSysUser().getUserId());
        DistributionUser dbDistributionUser = distributionUserService.getById(param.getDistributionUserId());
        distributionUserService.removeCacheByUserIdAndShopId(dbDistributionUser.getUserId(), dbDistributionUser.getShopId());
        distributionUserService.removeCacheByCardNo(dbDistributionUser.getCardNo());
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('distribution:distributionUser:delete')")
    public ResponseEntity<Void> delete(@RequestBody List<Long> ids) {
        distributionUserService.removeByIds(ids);
        return ResponseEntity.ok().build();
    }

}
