package cn.pluss.platform.controller.merchant;

import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.MerchantCashPlaceMapper;
import cn.pluss.platform.mapper.MerchantChannelMapper;
import cn.pluss.platform.merchant.MerchantCashPlaceService;
import cn.pluss.platform.merchant.MerchantCashPlaceStaffService;
import cn.pluss.platform.merchant.MerchantCashierCounteService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.vo.MerchantCashPlaceStaffVO;
import cn.pluss.platform.vo.MerchantCashPlaceVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 收银点
 *
 * @author DJH
 */
@RestController
@RequestMapping("/cashierCounte")
public class MerchantCashierCounteController {

    @Setter(onMethod_ = {@Autowired})
    private MerchantCashierCounteService merchantCashierCounteService;

    @Autowired
    private UserAppService userAppService;
    @Setter(onMethod_ = {@Autowired})
    private MerchantCashPlaceStaffService cashPlaceStaffService;
    @Resource
    private MerchantChannelMapper merchantChannelMapper;


    @PostMapping("/add")
    public Result<Object> add(@RequestBody MerchantCashPlace merchantCashPlace) {
        //判定字符位数
        if (StringUtils.isEmpty(merchantCashPlace.getAddress()) || StringUtils.isEmpty(merchantCashPlace.getName())) {
            throw new MsgException("地址或收银点名称不能为空");
        }
        UserApp userApp = userAppService.queryUserAppByToken();
        merchantCashPlace.setUserId(userApp.getUserId());
        merchantCashierCounteService.addCashierCounte(merchantCashPlace,userApp);
        return ResultGenerator.genSuccessResult("保存成功", merchantCashPlace);
    }

    /**
     * （新）收银点列表
     *
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/list")
    public Result<Object> list(@RequestParam Integer current, @RequestParam Integer size, @RequestParam String name) {
        UserApp userApp = userAppService.queryUserAppByToken();
        PageInfo result = merchantCashierCounteService.selectList(current,size,userApp.getUserId(),name);
        return ResultGenerator.genSuccessResult(result);
    }

    /**
     * 切换收银点
     *
     * @return
     */
    @GetMapping("/cutCashier")
    public Result<Object> cutCashier(@RequestParam long fromChannel,
                                     @RequestParam long toChannel) {
        Map<String,Object> map = merchantCashierCounteService.cutCashier(fromChannel, toChannel);
        return ResultGenerator.genSuccessResult(map);
    }




}