package cn.toesbieya.jxc.service.sys;

import cn.toesbieya.jxc.annoation.UserAction;
import cn.toesbieya.jxc.mapper.SysEquipmentMapper;
import cn.toesbieya.jxc.model.entity.SysEquipment;
import cn.toesbieya.jxc.model.vo.EquipmentVo;
import cn.toesbieya.jxc.model.vo.R;
import cn.toesbieya.jxc.model.vo.result.PageResult;
import cn.toesbieya.jxc.model.vo.search.EquipmentSearch;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

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

@Service
public class SysEquipmentService {
    @Resource
    private SysEquipmentMapper equipmentMapper;

    public PageResult<EquipmentVo> search(EquipmentSearch vo){
        Integer id = vo.getId();
        String name = vo.getName();
        String spec = vo.getSpec();
        String introduction = vo.getIntroduction();
        String supplier = vo.getSupplier();
        String location = vo.getLocation();
        Boolean enable = vo.getEnable();
        String manuDate = vo.getManuDate();
        String buyDate = vo.getBuyDate();
        Long startTime = vo.getStartTime();
        Long endTime = vo.getEndTime();

        Wrapper<SysEquipment> wrapper =
                Wrappers.lambdaQuery(SysEquipment.class)
                        .eq(id != null,SysEquipment::getId,id)
                        .like(!StringUtils.isEmpty(name),SysEquipment::getName,name)
                        .like(!StringUtils.isEmpty(spec),SysEquipment::getSpec,spec)
                        .like(!StringUtils.isEmpty(introduction),SysEquipment::getIntroduction,introduction)
                        .like(!StringUtils.isEmpty(supplier),SysEquipment::getSupplier,supplier)
                        .like(!StringUtils.isEmpty(location),SysEquipment::getLocation,location)
                        .like(!StringUtils.isEmpty(manuDate),SysEquipment::getManuDate,manuDate)
                        .like(!StringUtils.isEmpty(buyDate),SysEquipment::getBuyDate,buyDate)
                        .eq(enable != null,SysEquipment::isEnable,enable)
                        .ge(startTime != null,SysEquipment::getCreateTime,startTime)
                        .le(endTime != null,SysEquipment::getCreateTime,endTime)
                        .orderByDesc(SysEquipment::getCreateTime);

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

        List<SysEquipment> equipments = equipmentMapper.selectList(wrapper);

        PageResult<SysEquipment> pageResult = new PageResult<>(equipments);

        int equipmentNum = equipments.size();

        List<EquipmentVo> list = new ArrayList<>(equipmentNum);

        equipments.forEach(equipment -> {
            list.add(new EquipmentVo(equipment));
        });

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

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

    @UserAction("'修改设备' + #equipment.name")
    public R update(SysEquipment equipment){
        Integer id = equipment.getId();
        String name = equipment.getName();

        if(isNameExist(name,id)) {
            return R.fail(String.format("修改失败，设备【%s】已存在", name));
        }
        equipmentMapper.update(
                null,
                Wrappers.lambdaUpdate(SysEquipment.class)
                        .set(SysEquipment::getName, name)
                        .set(SysEquipment::getSupplier, equipment.getSupplier())
                        .set(SysEquipment::getIntroduction, equipment.getIntroduction())
                        .set(SysEquipment::getManuDate,equipment.getManuDate())
                        .set(SysEquipment::getBuyDate,equipment.getBuyDate())
                        .set(SysEquipment::getSpec, equipment.getSpec())
                        .set(SysEquipment::getLocation, equipment.getLocation())
                        .set(SysEquipment::isEnable, equipment.isEnable())
                        .eq(SysEquipment::getId, id)
        );
        return R.success("修改成功");
    }

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

    private boolean isNameExist(String name, Integer id) {
        Integer num = equipmentMapper.selectCount(
                Wrappers.lambdaQuery(SysEquipment.class)
                        .eq(SysEquipment::getName, name)
                        .ne(id != null, SysEquipment::getId, id)
        );

        return num != null && num > 0;
    }
}
