package com.yiqi.admin.controller.core;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.annotation.Log;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AjaxResult;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.enums.BusinessType;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.core.domain.ClothWashModeRelation;
import com.yiqi.core.domain.vo.ClothWashModeRelationVO;
import com.yiqi.core.service.IClothWashModeRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 洗衣方式和衣服类别关联Controller
 *
 * @author xiaohei
 * @date 2023-10-11
 */
@Api(tags = "管理后台 - 洗衣方式和衣服类别关联")
@RestController
@RequestMapping("/mapi/cloth/WashModeRelation")
public class ClothWashModeRelationController extends BaseController {
    @Autowired
    private IClothWashModeRelationService clothWashModeRelationService;

    /**
     * 获取洗衣方式和衣服类别关联详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取洗衣方式和衣服类别关联详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long washModeId) {
        return success(clothWashModeRelationService.selectClothWashModeRelationById(washModeId));
    }

    /**
     * 新增洗衣方式和衣服类别关联
     */
    @PreAuthorize("@ss.hasPermi('cloth:washMode:add')")
    @Log(title = "洗衣方式和衣服类别关联", businessType = BusinessType.INSERT)
    @PostMapping("/saveBatchRelation")
    @ApiOperation(value = "新增洗衣方式和衣服类别关联")
    public R saveBatchRelation(@RequestBody ClothWashModeRelationVO clothWashModeRelationVO) {
        clothWashModeRelationService.remove(new QueryWrapper<ClothWashModeRelation>().lambda()
                .eq(ClothWashModeRelation::getWashModeId, clothWashModeRelationVO.getWashModeId()));
        List<ClothWashModeRelation> relations = new ArrayList<>();
        if (CollUtil.isEmpty(clothWashModeRelationVO.getClothTypeIdList())) {
            R.fail();
        }
        for (Long id : clothWashModeRelationVO.getClothTypeIdList()) {
            ClothWashModeRelation clothWashModeRelation = new ClothWashModeRelation();
            clothWashModeRelation.setWashModeId(clothWashModeRelationVO.getWashModeId());
            clothWashModeRelation.setClothTypeId(id);
            clothWashModeRelation.setCreateById(getUserId());
            clothWashModeRelation.setCreateBy(getUsername());
            clothWashModeRelation.setCreateTime(DateUtils.getNowDate());
            relations.add(clothWashModeRelation);
        }
        clothWashModeRelationService.saveBatch(relations);
        return R.ok();
    }


}
