package vip.xiaonuo.production.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.production.entity.ProduceLocation;
import vip.xiaonuo.production.mapper.ProduceLocationMapper;
import vip.xiaonuo.production.param.ProductionLocationEditParam;
import vip.xiaonuo.production.param.ProductionLocationParam;
import vip.xiaonuo.production.service.IProduceLocationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.sys.modular.org.entity.SysOrg;
import vip.xiaonuo.sys.modular.org.mapper.SysOrgMapper;

import java.util.Map;

/**
 * <p>
 * 生成管控公司与车间对应表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-12
 */
@Service
public class ProduceLocationServiceImpl extends ServiceImpl<ProduceLocationMapper, ProduceLocation> implements IProduceLocationService {

    @Autowired
    private ProduceLocationMapper locationMapper;
    @Autowired
    private SysOrgMapper orgMapper;

    @Override
    public CommonResult add(ProductionLocationParam param) {
        String orgId = param.getOrgId();
        if ("".equals(orgId) || null == orgId) {
            return CommonResult.error("未选择车间所属公司");
        }
        SysOrg sysOrg = orgMapper.selectById(param.getOrgId());
        ProduceLocation produceLocation = new ProduceLocation();
        Long count = locationMapper.selectCount(new QueryWrapper<ProduceLocation>()
                .eq("org_id", param.getOrgId()).eq("location_name", param.getLocationName()));
        if (count == null||count==0) {
            produceLocation.setLocationName(param.getLocationName());
            produceLocation.setOrgId(param.getOrgId());
            produceLocation.setOrgName(sysOrg.getName());
            produceLocation.setEnergyId(param.getEnergyId());
            locationMapper.insert(produceLocation);
            return CommonResult.ok();
        } else {
            return CommonResult.error("车间名称已存在！");
        }
    }

    @Override
    public CommonResult list(Map<String, String> paramsMap) {
        String orgId = paramsMap.get("orgId");
        return CommonResult.data(locationMapper.selectList((new QueryWrapper<ProduceLocation>()
                .eq(StringUtils.isNotBlank(orgId),"org_id", orgId))));
    }

    @Override
    public CommonResult edit(ProductionLocationEditParam editParam) {
        Integer id = editParam.getId();
        Long count = locationMapper.selectCount(new QueryWrapper<ProduceLocation>()
                .eq("org_id", editParam.getOrgId()).eq("location_name", editParam.getLocationName())
                .ne("id",id));
        if (count == null||count==0) {
            ProduceLocation location = locationMapper.selectById(id);
            location.setLocationName(editParam.getLocationName());
            location.setEnergyId(editParam.getEnergyId());
            locationMapper.updateById(location);
            return CommonResult.ok();
        } else {
            return CommonResult.error("车间名称已存在！");
        }
    }

    @Override
    public CommonResult delete(Map<String, String> paramsMap) {
        locationMapper.deleteById(paramsMap.get("id"));
        return CommonResult.ok();
    }

    @Override
    public CommonResult page(Map<String, String> paramsMap) {
        String orgId = paramsMap.get("orgId");
        String pageNum = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        Page<ProduceLocation> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        QueryWrapper<ProduceLocation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        return CommonResult.data(locationMapper.selectPage(page,queryWrapper));
    }

}
