package cn.pluss.platform.controller.deviceVoiceBox;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.deviceVoiceBox.DeviceVoiceBoxService;
import cn.pluss.platform.entity.DeviceVoiceBox;
import cn.pluss.platform.entity.MerchantBaseInfo;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 智能音箱设备信息表 前端控制器
 * </p>
 *
 * @author bzg
 * @since 2020-11-04
 */
@Controller
@RequestMapping("/deviceVoiceBox")
public class DeviceVoiceBoxController extends BaseModelController<DeviceVoiceBoxService, DeviceVoiceBox> {

    @Autowired
    private MerchantBaseInfoService merchantBaseInfoService;

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/index")
    public String index() {
        return "deviceVoiceBox/list";
    }

    @PostMapping("saveOrUpdate")
    @ResponseBody
    public Result<String> saveOrUpdate(@RequestBody DeviceVoiceBox deviceVoiceBox) {
        if (deviceVoiceBox.getId() != null){
            if(StringUtil.isNotEmpty(deviceVoiceBox.getActMercId()) && StringUtil.isEmpty(deviceVoiceBox.getActMercName())){
                MerchantBaseInfo byId = merchantBaseInfoService.getById(deviceVoiceBox.getActMercId());
                if(byId == null){
                    return ResultGenerator.genFailResult("商户ID输入有误！");
                }
                deviceVoiceBox.setActMercName(byId.getAlias());
            }
            deviceVoiceBox.setUpdateTime(new Date());
            this.baseService.updateById(deviceVoiceBox);
        }else {
            if(StringUtil.isEmpty(deviceVoiceBox.getBoxNo())){
                return ResultGenerator.genFailResult("设备编号不能为空！");
            }
            QueryWrapper<DeviceVoiceBox> queryWrapper = new QueryWrapper<DeviceVoiceBox>()
                    .eq("boxNo",deviceVoiceBox.getBoxNo());
            List<DeviceVoiceBox> boxList = this.baseService.list(queryWrapper);
            if(!boxList.isEmpty()){
                return ResultGenerator.genFailResult("设备编号已存在，请重新添加！");
            }
            deviceVoiceBox.setCreateTime(new Date());
            this.baseService.save(deviceVoiceBox);
        }
        return ResultGenerator.genSuccessResult("保存成功",null);
    }

    @PostMapping("batchSaveNo")
    @ResponseBody
    public Result<String> batchSaveNo(@RequestBody DeviceVoiceBox deviceVoiceBox) {
        if(StringUtil.isEmpty(deviceVoiceBox.getStartBoxNo())){
            return ResultGenerator.genFailResult("开始编号不能为空，请重新输入！");
        }
        if(StringUtil.isEmpty(deviceVoiceBox.getEndBoxNo())){
            return ResultGenerator.genFailResult("结束编号不能为空，请重新输入！");
        }
        try {
            Integer startBoxNo = Integer.parseInt(deviceVoiceBox.getStartBoxNo());
            Integer endBoxNo = Integer.parseInt(deviceVoiceBox.getEndBoxNo());
            if(endBoxNo.compareTo(startBoxNo) < 0){
                return ResultGenerator.genFailResult("结束编号范围有误，请重新输入！");
            }
            List<DeviceVoiceBox> list = new ArrayList<>();
            for (int i = startBoxNo;i<= endBoxNo;i++) {
                DeviceVoiceBox box = new DeviceVoiceBox();
                box.setCreateTime(new Date());
                box.setBoxNo(String.valueOf(i));
                list.add(box);
            }
            this.baseService.saveBatch(list);
            return ResultGenerator.genSuccessResult("操作成功！！",null);
        }catch (Exception e){
            throw new MsgException("包含重复的设备编号！");
        }
    }


    @PostMapping("unBindSave")
    @ResponseBody
    public Result<String> unBindSave(@RequestBody DeviceVoiceBox deviceVoiceBox) {
        if(StringUtil.isEmpty(deviceVoiceBox.getId())){
            return ResultGenerator.genFailResult("解绑出错");
        }
        if(StringUtil.isEmpty(deviceVoiceBox.getStatus())){
            return ResultGenerator.genFailResult("解绑出错");
        }
        if("1".equals(deviceVoiceBox.getStatus())){
            deviceVoiceBox.setActMercName("");
            deviceVoiceBox.setActMercId(null);
        }
        this.baseService.updateById(deviceVoiceBox);
        return ResultGenerator.genSuccessResult("操作成功！！",null);
    }
}
