package cn.pluss.platform.controller.merchant;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.entity.MerchantCashPayCode;
import cn.pluss.platform.entity.MerchantCashPlace;
import cn.pluss.platform.entity.MerchantCashPlaceStaff;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.merchant.MerchantCashPlaceService;
import cn.pluss.platform.merchant.MerchantCashPlaceStaffService;
import cn.pluss.platform.vo.MerchantCashPlaceStaffVO;
import cn.pluss.platform.vo.MerchantCashPlaceVO;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author DJH
 */
@RestController
@RequestMapping("/merchantCashPlace")
public class MerchantCashPlaceController {

    @Setter(onMethod_ = {@Autowired})
    private MerchantCashPlaceService cashPlaceService;

    @Setter(onMethod_ = {@Autowired})
    private MerchantCashPlaceStaffService cashPlaceStaffService;

    @PostMapping
    public Result<Object> add(@RequestBody MerchantCashPlace merchantCashPlace) {
        //判定字符位数
        size(merchantCashPlace.getAddress());
        size(merchantCashPlace.getName());
        cashPlaceService.saveOrUpdate(merchantCashPlace);
        return ResultGenerator.genSuccessResult("保存成功", merchantCashPlace);
    }

    @GetMapping("/{code}")
    public Result<Object> delByCode(@PathVariable("code") String code) {
        cashPlaceService.delByCodeForApp(code);
        return ResultGenerator.genSuccessResult("操作成功", null);
    }

    @GetMapping("/list")
    public Result<Object> list(Long current, Long size, MerchantCashPlace merchantCashPlace) {
        Page<MerchantCashPlace> page = new Page<>();
        if (current == null) {
            page.setSize(100);
        } else {
            page.setCurrent(current);
            page.setSize(size);
        }
        List<MerchantCashPlaceVO> result = cashPlaceService.getListForApp(page, merchantCashPlace);
        return ResultGenerator.genSuccessResult(result);
    }

    @PostMapping("/payCode")
    public Result<Object> bindPayCode(@RequestBody MerchantCashPayCode merchantCashPayCode) {
        cashPlaceService.bindPayCode(merchantCashPayCode);
        return ResultGenerator.genSuccessResult("操作成功", null);
    }

    @GetMapping("/payCode/{payCode}")
    public Result<Object> unbindPayCode(@PathVariable("payCode") String payCode) {
        cashPlaceService.unbindPayCode(payCode);
        return ResultGenerator.genSuccessResult("操作成功", null);
    }


    @PostMapping("/payCode/online")
    public Result<Object> online(@RequestBody Map<String, Object> map) {
        Long userId = Long.parseLong((String) map.get("userId"));
        String cashPlaceCode = (String) map.get("cashPlaceCode");
        cashPlaceStaffService.bindCashPlace(userId, cashPlaceCode);

        return ResultGenerator.genSuccessResult("操作成功", null);
    }

    @GetMapping("/payCode/offline/{userId}")
    public Result<Object> offline(@PathVariable("userId") Long userId) {
        cashPlaceStaffService.offLine(userId);
        return ResultGenerator.genSuccessResult("操作成功", null);
    }

    @GetMapping("/payCode/status/{payCode}")
    public Result<Object> status(@PathVariable("payCode") String payCode) {
        MerchantCashPlaceStaffVO result = cashPlaceService.getStatusByPayCode(payCode);
        return ResultGenerator.genSuccessResult(result);
    }

    @GetMapping("/payCode/staff/{staffId}")
    public Result<Object> statusByStaffCode(@PathVariable("staffId") String staffId) {
        MerchantCashPlaceStaffVO result = cashPlaceService.getStatusByStaffId(staffId);
        return ResultGenerator.genSuccessResult(result);
    }

    @GetMapping("/record")
    public Result<Object> record(Page<MerchantCashPlaceStaff> page, MerchantCashPlaceStaff condition, OrderItem orderItem) {
        Page<MerchantCashPlaceStaffVO> pageData = cashPlaceStaffService.getRecord(page, condition, orderItem);
        return ResultGenerator.genSuccessResult(pageData);
    }
    private void size(String character){
        if (character.length() >= 50){
            throw new MsgException("输入内容过长");
        }
    }
}