package com.flossom.system.controller;

import com.flossom.common.core.domain.entity.WxInstrument;
import com.flossom.common.core.domain.req.WxInstrumentReq;
import com.flossom.common.core.domain.req.WxInstrumentSaveReq;
import com.flossom.common.core.exception.ServiceException;
import com.flossom.common.core.utils.StringUtils;
import com.flossom.common.core.web.controller.BaseController;
import com.flossom.common.core.web.domain.AjaxResult;
import com.flossom.common.core.web.page.TableDataInfo;
import com.flossom.common.log.annotation.Log;
import com.flossom.common.log.enums.BusinessType;
import com.flossom.common.security.annotation.RequiresPermissions;
import com.flossom.common.security.utils.SecurityUtils;
import com.flossom.system.service.IWxInstrumentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 仪器列Controller
 *
 * @author flossom
 * @date 2024-01-06
 */
@RestController
@RequestMapping("/instrument")
public class WxInstrumentController extends BaseController {

    @Autowired
    private IWxInstrumentService wxInstrumentService;

    /**
     * 查询仪器列列表
     */
    @RequiresPermissions("system:instrument:list")
    @GetMapping("/list")
    public TableDataInfo list(WxInstrumentReq wxInstrumentReq) {
        startPage();
        List<WxInstrument> list = wxInstrumentService.selectWxInstrumentPage(wxInstrumentReq);
        return getDataTable(list);
    }

    /**
     * 获取全部的仪器
     */
    @GetMapping("/getAllInstrument")
    public AjaxResult getAllInstrument() {
        return success(wxInstrumentService.selectWxInstrumentList(new WxInstrument()));
    }

    /**
     * 获取仪器列详细信息
     */
    @RequiresPermissions("system:instrument:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(wxInstrumentService.selectWxInstrumentReqById(id));
    }

    /**
     * 新增仪器列
     */
    @RequiresPermissions("system:instrument:add")
    @Log(title = "仪器列", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxInstrumentSaveReq wxInstrumentSaveReq) {
        // 开启额外打开奖励
        if (wxInstrumentSaveReq.getIsExtraClock() == 1) {
            if (wxInstrumentSaveReq.getExtraClockCredit() == null
                    || wxInstrumentSaveReq.getExtraClockTimeRange() == null
                    || wxInstrumentSaveReq.getExtraClockTimeRange().size() != 2) {
                throw new ServiceException("请输入额外打卡奖励或者选择额外打卡时间");
            }
        } else {
            wxInstrumentSaveReq.setExtraClockCredit(null);
            wxInstrumentSaveReq.setExtraClockTimeRange(null);
        }

        // 仪器类型:IOT仪器
        if (wxInstrumentSaveReq.getType() == 2) {
            if (StringUtils.isBlank(wxInstrumentSaveReq.getModel())) {
                throw new ServiceException("请选择仪器类型");
            }
        } else {
            wxInstrumentSaveReq.setModel(null);
        }

        // 支持购买
        if (wxInstrumentSaveReq.getIsPurchase() == 1) {
            if (StringUtils.isBlank(wxInstrumentSaveReq.getShoppingAppid())
                    || StringUtils.isBlank(wxInstrumentSaveReq.getShoppingPath())) {
                throw new ServiceException("请输入购买商城APPID和商城PATH");
            }
        } else {
            wxInstrumentSaveReq.setShoppingAppid(null);
            wxInstrumentSaveReq.setShoppingPath(null);
        }
        return toAjax(wxInstrumentService.addWxInstrument(wxInstrumentSaveReq));
    }

    /**
     * 修改仪器列
     */
    @RequiresPermissions("system:instrument:edit")
    @Log(title = "仪器列", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxInstrumentSaveReq wxInstrumentSaveReq) {
        return toAjax(wxInstrumentService.updateWxInstrument(wxInstrumentSaveReq));
    }

    /**
     * 隐藏仪器
     */
    @RequiresPermissions("system:instrument:remove")
    @Log(title = "仪器列", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(wxInstrumentService.deleteWxInstrumentByIds(id));
    }
}
