package com.ruoyi.web.v2.v1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.v2.v1.model.JsWorkshop;
import com.ruoyi.web.v2.v1.mapper.JsWorkshopMapper;
import com.ruoyi.web.v2.v1.service.IJsWorkshopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * <p>
 * v2.0生产车间管理 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-05-21
 */
@Service
public class JsWorkshopServiceImpl extends ServiceImpl<JsWorkshopMapper, JsWorkshop> implements IJsWorkshopService {

    @Autowired
    private JsWorkshopMapper workshopMapper;

    @Override
    public AjaxResult add(JsWorkshop workshop) {
        String workshopName = workshop.getWorkshopName();
        Integer count = workshopMapper.selectCount(new QueryWrapper<JsWorkshop>().eq("workshop_name", workshopName));
        if (count != 0) {
            return error("车间名称已存在！");
        } else {
//            JsWorkshop workshop1 = workshopMapper.selectOne(new QueryWrapper<JsWorkshop>().orderByDesc("id").last(" limit 1"));
//            if (ObjectUtil.isEmpty(workshop1)) {
//                String formattedNumber = String.format("%03d", 1);
//                workshop.setWorkshopNum(formattedNumber);
//            } else {
//                String workshopNum = workshop1.getWorkshopNum();
//                Integer integer = Integer.valueOf(workshopNum);
//                String formattedNumber = String.format("%03d", integer+1);
//                workshop.setWorkshopNum(formattedNumber);
//            }
            workshopMapper.insert(workshop);
        }
        return success("添加成功");
    }

    @Override
    public AjaxResult edit(JsWorkshop workshop) {
        String workshopName = workshop.getWorkshopName();
        Integer count = workshopMapper.selectCount(new QueryWrapper<JsWorkshop>().eq("workshop_name", workshopName).ne("id",workshop.getId()));
        if (count != 0) {
            return error("车间名称已存在！");
        } else {
            workshopMapper.updateById(workshop);
        }
        return success("修改成功");
    }

    @Override
    public AjaxResult delete(String ids) {
        String[] split = ids.split(",");
        for (String s : split) {
            workshopMapper.deleteById(s);
        }
        return success("删除成功");
    }

    @Override
    public AjaxResult page(Integer pageNum, Integer pageSize, String workshopName, String workshopNum, String workshopType) {
        QueryWrapper<JsWorkshop> queryWrapper = new QueryWrapper<>();
        Page<JsWorkshop> page = new Page<>(pageNum, pageSize);
        queryWrapper.like(StringUtils.isNotEmpty(workshopName), "workshop_name", workshopName).or()
                .eq(StringUtils.isNotEmpty(workshopNum), "workshop_num", workshopNum).or()
                .eq(StringUtils.isNotEmpty(workshopType), "workshop_type", workshopType);
        return success(workshopMapper.selectPage(page, queryWrapper));
    }

    @Override
    public AjaxResult listAll(String workshopType) {
        if ("".equals(workshopType) || null==workshopType) {
            return success(workshopMapper.selectList(null));
        } else {
            return success(workshopMapper.selectList(new QueryWrapper<JsWorkshop>().eq("workshop_type",workshopType)));
        }

    }


}
