package com.ruoyi.web.controller.app;

import com.fasterxml.jackson.annotation.JsonView;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.JsonViewProfile;
import com.ruoyi.common.core.domain.ValidGroup;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.device.service.DeviceValidator;
import com.ruoyi.ss.deviceSuit.service.DeviceSuitService;
import com.ruoyi.ss.suit.domain.Suit;
import com.ruoyi.ss.suit.domain.SuitBO;
import com.ruoyi.ss.suit.domain.SuitQuery;
import com.ruoyi.ss.suit.domain.SuitVO;
import com.ruoyi.ss.suit.service.SuitAssembler;
import com.ruoyi.ss.suit.service.SuitService;
import com.ruoyi.ss.suit.service.SuitValidator;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * @author wjh
 * 2024/5/8
 */
@RestController
@RequestMapping("/app/suit")
public class AppSuitController extends BaseController {

    @Autowired
    private SuitService suitService;

    @Autowired
    private SuitValidator suitValidator;

    @Autowired
    private DeviceValidator deviceValidator;

    @Autowired
    private SuitAssembler suitAssembler;

    @Autowired
    private DeviceSuitService deviceSuitService;

    @ApiOperation("查询设备套餐列表")
    @JsonView(JsonViewProfile.AppMch.class)
    @GetMapping("/listByDeviceId/{deviceId}")
    public AjaxResult list(@PathVariable Long deviceId) {
        ServiceUtil.assertion(!deviceValidator.isMch(deviceId, getUserId()), "这不是您的设备，无法查看");
        List<SuitVO> list = suitService.selectByDeviceId(deviceId);
        suitAssembler.assembleDeviceList(list);
        return success(list);
    }

    @ApiOperation("查询可作为模板的套餐列表")
    @GetMapping("/tempList")
    @JsonView(JsonViewProfile.AppMch.class)
    public TableDataInfo tempList(SuitQuery query) {
        query.setUserId(getUserId());
        startPage();
        List<SuitVO> list = suitService.selectSuitList(query);
        suitAssembler.assembleDeviceList(list);
        return getDataTable(list);
    }

    @ApiOperation("获取套餐详细信息")
    @JsonView(JsonViewProfile.AppMch.class)
    @GetMapping("/{suitId}")
    public AjaxResult getInfo(@PathVariable("suitId") Long suitId) {
        SuitVO suit = suitService.selectSuitBySuitId(suitId);
        List<SuitVO> list = Collections.singletonList(suit);
        suitAssembler.assembleDeviceList(list);
        return success(suit);
    }

    @ApiOperation("新增套餐")
    @PostMapping
    public AjaxResult add(@RequestBody @Validated({ValidGroup.FrontCreate.class}) SuitBO suit) {
        suit.setUserId(getUserId());
        suit = suit.filterCreateByApp();
        ServiceUtil.assertion(suitValidator.preCreateByApp(suit));
        return success(suitService.insertSuit(suit));
    }

    @ApiOperation("修改套餐")
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated({ValidGroup.FrontUpdate.class}) SuitBO suit) {
        suit = suit.filterUpdateByApp();
        ServiceUtil.assertion(suitValidator.preUpdateByApp(suit));
        return success(suitService.updateSuit(suit));
    }

    @ApiOperation("删除套餐")
    @DeleteMapping("/{suitIds}")
    public AjaxResult remove(@PathVariable @ApiParam("套餐id列表") List<Long> suitIds) {
        ServiceUtil.assertion(suitValidator.preLogicDelByApp(suitIds));
        return success(suitService.logicDel(suitIds) >= 1);
    }

    @ApiOperation("将指定设备的套餐清空")
    @DeleteMapping("/delByDevice/{deviceId}")
    public AjaxResult delByDevice(@PathVariable Long deviceId) {
        if (!deviceValidator.isMch(deviceId, getUserId())) {
            return error("这不是您的设备，无法清空套餐");
        }
        return toAjax(deviceSuitService.deleteByDeviceId(deviceId));
    }

    @ApiOperation("套餐排序")
    @PutMapping("/sort")
    public AjaxResult setSort(@RequestBody List<Suit> list) {
        if (!suitValidator.isBelong(CollectionUtils.map(list, Suit::getSuitId), getUserId())) {
            return error("不允许操作不属于自己的套餐");
        }
        return toAjax(suitService.setSort(list));
    }

}
