package com.fjhx.service.production.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.base.BaseEntity;
import com.fjhx.entity.production.ProductionWorkshop;
import com.fjhx.params.production.ProductionWorkshopEx;
import com.fjhx.params.production.ProductionWorkshopVo;
import com.fjhx.mapper.production.ProductionWorkshopMapper;
import com.fjhx.service.production.ProductionWorkshopService;
import com.fjhx.utils.HxBeanUtil;
import com.fjhx.utils.UserClientUtil;
import com.fjhx.utils.WrapperUtil;
import org.springblade.core.secure.utils.AuthUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 生产车间 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-07-13
 */
@Service
public class ProductionWorkshopServiceImpl extends ServiceImpl<ProductionWorkshopMapper, ProductionWorkshop> implements ProductionWorkshopService {

    @Override
    public Page<ProductionWorkshopEx> getPage(Map<String, String> condition) {

        QueryWrapper<ProductionWorkshop> wrapper = Wrappers.query();

        WrapperUtil.init(condition, wrapper)
                .like("name", "keyword") // 关键字（目前只有车间名称）
                .eq("status", "status") // 车间状态
                .createTimeDesc();

        Page<ProductionWorkshop> page = page(condition, wrapper);

        // 拓展返回值
        Page<ProductionWorkshopEx> result = HxBeanUtil.copyPageEx(page, ProductionWorkshopEx.class);

        // 赋值车间负责人名称
        List<ProductionWorkshopEx> records = result.getRecords();
        Map<Long, String> userNameMap = UserClientUtil.getUserNameMap(records, ProductionWorkshop::getResponsibleUserId);
        records.forEach(item -> item.setResponsibleUserName(userNameMap.get(item.getResponsibleUserId())));

        return result;
    }

    @Override
    public void add(ProductionWorkshopVo productionWorkshopVo) {
        save(productionWorkshopVo);
    }

    @Override
    public void edit(ProductionWorkshopVo productionWorkshopVo) {
        updateById(productionWorkshopVo);
    }

    @Override
    public void delete(ProductionWorkshopVo productionWorkshopVo) {
        removeById(productionWorkshopVo.getId());
    }

    @Override
    public List<ProductionWorkshop> getList() {
        return lambdaQuery()
                .eq(BaseEntity::getTenantId, AuthUtil.getTenantId())
                .orderByDesc(BaseEntity::getId)
                .select(BaseEntity::getId, ProductionWorkshop::getName)
                .list();
    }

}
