package com.yiqi.admin.controller.core;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.exception.ServiceException;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.enums.DeleteStatus;
import com.yiqi.common.enums.StatusType;
import com.yiqi.core.domain.ClothFlaw;
import com.yiqi.core.service.IClothFlawService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
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 com.yiqi.common.annotation.Log;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AjaxResult;
import com.yiqi.common.enums.BusinessType;
import com.yiqi.core.domain.ClothFlawType;
import com.yiqi.core.service.IClothFlawTypeService;
import com.yiqi.common.core.page.TableDataInfo;

/**
 * 衣服瑕疵类型Controller
 *
 * @author ruoyi
 * @date 2023-08-07
 */
@Api(tags = "衣服瑕疵类型管理")
@RestController
@RequestMapping("/mapi/core/flawType")
public class ClothFlawTypeController extends BaseController {
    @Autowired
    private IClothFlawTypeService clothFlawTypeService;
    @Autowired
    private IClothFlawService clothFlawService;

    @ApiOperation("获取所有衣服瑕疵类型")
    @GetMapping(value = "all")
    public R<List<ClothFlawType>> all() throws Exception{
        return R.ok(clothFlawTypeService.list(new QueryWrapper<ClothFlawType>().lambda()
                .eq(ClothFlawType::getDelFlag, DeleteStatus.OK.getCode())
                .eq(ClothFlawType::getStatus, StatusType.OK.getCode())
                .orderByAsc(ClothFlawType::getSort)));
    }

    /**
    * 查询衣服瑕疵类型列表
    */
    @PreAuthorize("@ss.hasPermi('cloth:flawType:list')")
    @GetMapping("/list")
    public TableDataInfo list(ClothFlawType clothFlawType) {
        startPage();
        List<ClothFlawType> list = clothFlawTypeService.selectClothFlawTypeList(clothFlawType);
        return getDataTable(list);
    }

    /**
     * 获取衣服瑕疵类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('cloth:flawType:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(clothFlawTypeService.selectClothFlawTypeById(id));
    }

    /**
     * 新增衣服瑕疵类型
     */
    @PreAuthorize("@ss.hasPermi('cloth:flawType:add')")
    @Log(title = "衣服瑕疵类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClothFlawType clothFlawType) {
        return toAjax(clothFlawTypeService.insertClothFlawType(clothFlawType));
    }

    /**
     * 修改衣服瑕疵类型
     */
    @PreAuthorize("@ss.hasPermi('cloth:flawType:edit')")
    @Log(title = "衣服瑕疵类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ClothFlawType clothFlawType) {
        return toAjax(clothFlawTypeService.updateClothFlawType(clothFlawType));
    }

    /**
     * 删除衣服瑕疵类型
     */
    @PreAuthorize("@ss.hasPermi('cloth:flawType:remove')")
    @Log(title = "衣服瑕疵类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        if(clothFlawService.count(new QueryWrapper<ClothFlaw>().lambda().in(ClothFlaw::getFlawTypeId,ids)
                .eq(ClothFlaw::getDelFlag,DeleteStatus.OK.getCode())) > 0){
            throw new ServiceException("该类型下存在瑕疵数据，请先删除瑕疵后再删除类型");
        }
        return toAjax(clothFlawTypeService.deleteClothFlawTypeByIds(ids));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('cloth:flawType:edit')")
    @Log(title = "衣服瑕疵", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody ClothFlawType clothFlawType) {
        return toAjax(clothFlawTypeService.updateFlawTypeStatus(clothFlawType));
    }
}
