package com.yiqi.admin.controller.core;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.exception.ServiceException;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.enums.DeleteStatus;
import com.yiqi.common.enums.StatusType;
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.ClothAdjunct;
import com.yiqi.core.service.IClothAdjunctService;
import com.yiqi.common.utils.poi.ExcelUtil;
import com.yiqi.common.core.page.TableDataInfo;

/**
 * 衣服附件Controller
 *
 * @author xiaohei
 * @date 2023-08-20
 */
@Api(tags = "管理后台 - 衣服附件")
@RestController
@RequestMapping(UrlConstants.managerApi + "/cloth/adjunct")
public class ClothAdjunctController extends BaseController {
    @Autowired
    private IClothAdjunctService clothAdjunctService;

    @GetMapping(value = "all")
    @ApiOperation("获取所有衣服附件")
    public R<List<ClothAdjunct>> all() throws Exception{
        return R.ok(clothAdjunctService.list(new QueryWrapper<ClothAdjunct>().lambda()
                .eq(ClothAdjunct::getDelFlag,DeleteStatus.OK.getCode())
                .eq(ClothAdjunct::getStatus, StatusType.OK.getCode())));
    }
  /**
     * 查询衣服附件列表
     */
    @PreAuthorize("@ss.hasPermi('cloth:adjunct:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询衣服附件列表")
    public TableDataInfo list(ClothAdjunct clothAdjunct) {
        startPage();
        List<ClothAdjunct> list = clothAdjunctService.selectClothAdjunctList(clothAdjunct);
        return getDataTable(list);
    }

    /**
     * 导出衣服附件列表
     */
    @PreAuthorize("@ss.hasPermi('cloth:adjunct:export')")
    @Log(title = "衣服附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出衣服附件列表")
    public void export(HttpServletResponse response, ClothAdjunct clothAdjunct) {
        List<ClothAdjunct> list = clothAdjunctService.selectClothAdjunctList(clothAdjunct);
        ExcelUtil<ClothAdjunct> util = new ExcelUtil<ClothAdjunct>(ClothAdjunct. class);
        util.exportExcel(response, list, "衣服附件数据");
    }

    /**
     * 获取衣服附件详细信息
     */
    @PreAuthorize("@ss.hasPermi('cloth:adjunct:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取衣服附件详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(clothAdjunctService.selectClothAdjunctById(id));
    }

    /**
     * 新增衣服附件
     */
    @PreAuthorize("@ss.hasPermi('cloth:adjunct:add')")
    @Log(title = "衣服附件", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增衣服附件")
    public AjaxResult add(@RequestBody ClothAdjunct clothAdjunct) {
        if(clothAdjunctService.count(new QueryWrapper<ClothAdjunct>().lambda()
                .eq(ClothAdjunct::getCode,clothAdjunct.getCode())
                .eq(ClothAdjunct::getDelFlag, DeleteStatus.OK.getCode())) > 0){
            throw new ServiceException("衣服附件编号已存在");
        }
        clothAdjunct.setCreateById(getUserId());
        clothAdjunct.setCreateBy(getUsername());
        return toAjax(clothAdjunctService.insertClothAdjunct(clothAdjunct));
    }

    /**
     * 修改衣服附件
     */
    @PreAuthorize("@ss.hasPermi('cloth:adjunct:edit')")
    @Log(title = "衣服附件", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改衣服附件")
    public AjaxResult edit(@RequestBody ClothAdjunct clothAdjunct) {
        if(clothAdjunctService.count(new QueryWrapper<ClothAdjunct>().lambda()
                .eq(ClothAdjunct::getCode,clothAdjunct.getCode())
                .eq(ClothAdjunct::getDelFlag, DeleteStatus.OK.getCode())
                .ne(ClothAdjunct::getId,clothAdjunct.getId())) > 0){
            throw new ServiceException("衣服附件编号已存在");
        }
        clothAdjunct.setUpdateById(getUserId());
        clothAdjunct.setUpdateBy(getUsername());
        return toAjax(clothAdjunctService.updateClothAdjunct(clothAdjunct));
    }

    /**
     * 删除衣服附件
     */
    @PreAuthorize("@ss.hasPermi('cloth:adjunct:remove')")
    @Log(title = "衣服附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除衣服附件")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(clothAdjunctService.deleteClothAdjunctByIds(ids));
    }

    /**
     * 修改衣服附件状态
     */
    @PreAuthorize("@ss.hasPermi('cloth:adjunct:edit')")
    @Log(title = "衣服附件", businessType = BusinessType.UPDATE)
    @PostMapping(value = "updateStatus")
    @ApiOperation(value = "修改衣服附件状态")
    public AjaxResult updateClothAdjunctStatus(@RequestBody ClothAdjunct clothAdjunct) {
        return toAjax(clothAdjunctService.updateClothAdjunctStatus(clothAdjunct));
    }
}
