package cn.pluss.platform.controller.merchant;

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.MerchantChannelMapper;
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.api.R;
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 javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

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

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

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


    @PostMapping
    public Result<Object> add(@RequestBody MerchantCashPlace merchantCashPlace) {
        //判定字符位数
        if (Objects.equals(merchantCashPlace.getAddress(), null) || Objects.equals(merchantCashPlace.getName(), null)){
            throw new MsgException("地址或收银点名称不能为空");
        }
        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);
    }

    /**
     * （新）收银点列表
     * @param current
     * @param size
     * @param merchantCashPlace
     * @return
     */
    @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);
        List<MerchantCashPlaceVO> result = cashPlaceService.getListForBase(current.intValue(),size.intValue(), merchantCashPlace.getMerchantCode(),merchantCashPlace.getName());
        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("输入内容过长");
        }
    }

    /**
     * (新)增加收银点
     * @return
     */
    @PostMapping("/add")
    public Result<Object> add(@RequestBody MerchantBaseInfo merchantBaseInfo){
        //判定字符位数
        if (Objects.equals(merchantBaseInfo.getAddress(), null) || Objects.equals(merchantBaseInfo.getAlias(), null)){
            throw new MsgException("地址或收银点名称不能为空");
        }
        Boolean aBoolean = cashPlaceService.addCashPlace(merchantBaseInfo);
        if (aBoolean){
            return ResultGenerator.genSuccessResult("");
        }
        return ResultGenerator.genFailResult("增加失败");
    }

    /**
     * 通道详情
     * @return
     */
    @GetMapping("/channel")
    public Result<Object> channel(@RequestParam String merchantCode){
        List<MerchantChannel> merchantChannels = merchantChannelMapper.selectList();
        List<MerchantChannel> merchantChannelVo = cashPlaceService.merchantChannel(merchantCode, merchantChannels);
        return ResultGenerator.genSuccessResult(merchantChannelVo);
    }

    /**
     * 切换收银点
     * @return
     */
    @GetMapping("/cutChannel")
    public Result<Object> cutChannel(@RequestParam String merchantCode,
                                     @RequestParam String code,
                                     @RequestParam Integer channel){
        Boolean aBoolean = cashPlaceStaffService.cutChannel(merchantCode, code, channel);
        if (aBoolean){
            return ResultGenerator.genSuccessResult();
        }
        return ResultGenerator.genFailResult("无法切换该通道");
    }
}