package cn.toesbieya.jxc.service.sys;

import cn.toesbieya.jxc.annoation.UserAction;
import cn.toesbieya.jxc.mapper.SysStorehouseMapper;
import cn.toesbieya.jxc.model.entity.SysStorehouse;
import cn.toesbieya.jxc.model.vo.R;
import cn.toesbieya.jxc.model.vo.StorehouseVo;
import cn.toesbieya.jxc.model.vo.result.PageResult;
import cn.toesbieya.jxc.model.vo.search.StorehouseSearch;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysStorehouseService {
    @Resource
    private SysStorehouseMapper storehouseMapper;

    public List<SysStorehouse> get() {
        return storehouseMapper.selectList(
                Wrappers.lambdaQuery(SysStorehouse.class)
                        .eq(SysStorehouse::isEnable, true)
        );
    }

    public PageResult<StorehouseVo> search(StorehouseSearch vo){
        String id = vo.getId();
        String name = vo.getName();
        Integer level = vo.getLevel();
        Integer type = vo.getType();
        Boolean enable = vo.getEnable();
        Long startTime = vo.getStartTime();
        Long endTime = vo.getEndTime();

        Wrapper<SysStorehouse> wrapper =
                Wrappers.lambdaQuery(SysStorehouse.class)
                        .like(false, SysStorehouse::getId, id)
                        .like(!StringUtils.isEmpty(name), SysStorehouse::getName, name)
                        .eq(level != null, SysStorehouse::getLevel, level)
                        .eq(type != null, SysStorehouse::getType, type)

                        .eq(enable != null, SysStorehouse::isEnable, enable)
                        .ge(startTime != null, SysStorehouse::getCreateTime, startTime)
                        .le(endTime != null, SysStorehouse::getCreateTime, endTime)
                        .orderByDesc(SysStorehouse::getCreateTime);

        PageHelper.startPage(vo.getPage(), vo.getPageSize());

        List<SysStorehouse> storehouses = storehouseMapper.selectList(wrapper);

        PageResult<SysStorehouse> pageResult = new PageResult<>(storehouses);

        int storehouseNum = storehouses.size();

        List<StorehouseVo> list = new ArrayList<>(storehouseNum);

        storehouses.forEach(storehouse -> list.add(new StorehouseVo(storehouse)));

        return new PageResult<>(pageResult.getTotal(), list);
    }

    @UserAction("'添加仓库：'+ #storehouse.name")
    public R add(SysStorehouse storehouse) {
        if (isNameExist(storehouse.getName(), null)) {
            return R.fail(String.format("添加失败，仓库【%s】已存在", storehouse.getName()));
        }
        int rows = storehouseMapper.insert(storehouse);
        return rows > 0 ? R.success("添加成功") : R.fail("添加失败");
    }

    @UserAction("'修改仓库：'+ #storehouse.name")
    public R update(SysStorehouse storehouse) {
        Integer id = storehouse.getId();
        String name = storehouse.getName();

        if (isNameExist(name, id)) {
            return R.fail(String.format("修改失败，仓库【%s】已存在", name));
        }

        storehouseMapper.update(
                null,
                Wrappers.lambdaUpdate(SysStorehouse.class)
                        .set(SysStorehouse::getName, name)
                        .set(SysStorehouse::getLevel, storehouse.getLevel())
                        .set(SysStorehouse::getType, storehouse.getType())
                        .set(SysStorehouse::isEnable, storehouse.isEnable())
                        //添加修改人   .set(SysStorehouse::getUpdateId, storehouse.getRegion())
                        //添加修改时间  .set(SysStorehouse::getUpdateTime, storehouse.isEnable())
                        .eq(SysStorehouse::getId, id)
        );
        return R.success("修改成功");
    }

    @UserAction("'删除仓库：'+ #storehouse.name")
    public R del(SysStorehouse storehouse) {
        int rows = storehouseMapper.delete(
                Wrappers.lambdaQuery(SysStorehouse.class)
                        .eq(SysStorehouse::getId, storehouse.getId())
                        .eq(SysStorehouse::isEnable, false)
        );
        return rows > 0 ? R.success("删除成功") : R.fail("删除失败，请刷新重试");
    }

    private boolean isNameExist(String name, Integer id) {
        Integer num = storehouseMapper.selectCount(
                Wrappers.lambdaQuery(SysStorehouse.class)
                        .eq(SysStorehouse::getName, name)
                        .ne(!StringUtils.isEmpty(id), SysStorehouse::getId, id)
        );
        return num != null && num > 0;
    }
}
