package com.yiqi.admin.controller.core;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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 com.yiqi.common.enums.SysBoolType;
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.ClothSpeed;
import com.yiqi.core.service.IClothSpeedService;
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/speed")
public class ClothSpeedController extends BaseController {
    @Autowired
    private IClothSpeedService clothSpeedService;

    /**
     * 加快的洗衣速度类型
     * @return
     */
    @GetMapping("/allSpeed")
    @ApiOperation(value = "加快的洗衣速度类型")
    public R<List<ClothSpeed>> allSpeed() {
        return R.ok(clothSpeedService.list(new QueryWrapper<ClothSpeed>().lambda()
                .eq(ClothSpeed::getDelFlag,DeleteStatus.OK.getCode())
                .eq(ClothSpeed::getStatus, StatusType.OK.getCode())
                .orderByDesc(ClothSpeed::getIsDefault)));
    }

    /**
     * 查询洗衣速度列表
     */
    @PreAuthorize("@ss.hasPermi('cloth:speed:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询洗衣速度列表")
    public TableDataInfo list(ClothSpeed clothSpeed) {
        startPage();
        List<ClothSpeed> list = clothSpeedService.selectClothSpeedList(clothSpeed);
        return getDataTable(list);
    }

    /**
     * 导出洗衣速度列表
     */
    @PreAuthorize("@ss.hasPermi('cloth:speed:export')")
    @Log(title = "洗衣速度", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出洗衣速度列表")
    public void export(HttpServletResponse response, ClothSpeed clothSpeed) {
        List<ClothSpeed> list = clothSpeedService.selectClothSpeedList(clothSpeed);
        ExcelUtil<ClothSpeed> util = new ExcelUtil<ClothSpeed>(ClothSpeed. class);
        util.exportExcel(response, list, "洗衣速度数据");
    }

    /**
     * 获取洗衣速度详细信息
     */
    @PreAuthorize("@ss.hasPermi('cloth:speed:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取洗衣速度详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(clothSpeedService.selectClothSpeedById(id));
    }

    /**
     * 新增洗衣速度
     */
    @PreAuthorize("@ss.hasPermi('cloth:speed:add')")
    @Log(title = "洗衣速度", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增洗衣速度")
    public AjaxResult add(@Valid @RequestBody ClothSpeed clothSpeed) {
        clothSpeed.setCreateById(getUserId());
        clothSpeed.setCreateBy(getUsername());

        //如果是设置为默认，要将其他的设置为非默认
        if(SysBoolType.YES.getCode().equals(clothSpeed.getIsDefault())){
            clothSpeedService.update(new UpdateWrapper<ClothSpeed>().lambda().eq(ClothSpeed::getDelFlag, DeleteStatus.OK.getCode())
                    .eq(ClothSpeed::getIsDefault,SysBoolType.YES.getCode()).set(ClothSpeed::getIsDefault,SysBoolType.NO.getCode()));
        }
        return toAjax(clothSpeedService.insertClothSpeed(clothSpeed));
    }

    /**
     * 修改洗衣速度
     */
    @PreAuthorize("@ss.hasPermi('cloth:speed:edit')")
    @Log(title = "洗衣速度", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改洗衣速度")
    public AjaxResult edit(@Valid @RequestBody ClothSpeed clothSpeed) {
        clothSpeed.setUpdateById(getUserId());
        clothSpeed.setUpdateBy(getUsername());

        //如果是设置为默认，要将其他的设置为非默认
        if(SysBoolType.YES.getCode().equals(clothSpeed.getIsDefault())){
            clothSpeedService.update(new UpdateWrapper<ClothSpeed>().lambda()
                    .eq(ClothSpeed::getDelFlag, DeleteStatus.OK.getCode())
                    .eq(ClothSpeed::getIsDefault,SysBoolType.YES.getCode())
                    .ne(ClothSpeed::getId,clothSpeed.getId())
                    .set(ClothSpeed::getIsDefault,SysBoolType.NO.getCode()));
        }
        return toAjax(clothSpeedService.updateClothSpeed(clothSpeed));
    }

    /**
     * 删除洗衣速度
     */
    @PreAuthorize("@ss.hasPermi('cloth:speed:remove')")
    @Log(title = "洗衣速度", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除洗衣速度")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(clothSpeedService.deleteClothSpeedByIds(ids));
    }

    /**
     * 修改洗衣速度状态
     */
    @PreAuthorize("@ss.hasPermi('cloth:speed:edit')")
    @Log(title = "洗衣速度", businessType = BusinessType.UPDATE)
    @PostMapping(value = "updateStatus")
    @ApiOperation(value = "修改洗衣速度状态")
    public AjaxResult updateClothSpeedStatus(@RequestBody ClothSpeed clothSpeed) {
        return toAjax(clothSpeedService.updateClothSpeedStatus(clothSpeed));
    }
}
