package com.zd.bottle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zd.bottle.domain.RfidTag;
import com.zd.bottle.mapper.RfidTagMapper;
import com.zd.bottle.service.RfidTagService;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.security.service.TokenService;
import com.zd.system.api.domain.SysUser;
import com.zd.system.api.model.LoginUser;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * RFID标签管理 服务实现类
 * </p>
 *
 * @author hanson
 * @since 2022-09-08
 */
@Service
public class RfidTagServiceImpl extends ServiceImpl<RfidTagMapper, RfidTag> implements RfidTagService {

    @Resource
    private TokenService tokenService;

    @Resource
    private RfidTagMapper rfidTagMapper;

    @Override
    public RfidTag add(RfidTag rfidTag) {
        String username=null;
        Long userId=null;
        Long deptId=null;
        LoginUser loginUser = tokenService.getLoginUser();
        if (loginUser!=null){
            username=loginUser.getUsername();
            SysUser sysUser = loginUser.getSysUser();
            userId=sysUser.getUserId();
            deptId=sysUser.getDeptId();
        }
        QueryWrapper<RfidTag> wrapper = Wrappers.query();
        wrapper.select("  IFNULL( max(auth_code),0) as authCode");
        Map<String, Object> map = getMap(wrapper);
        int authCode = Integer.parseInt((String) map.get("authCode"));
        authCode+=1;
        DecimalFormat gf=new DecimalFormat("0000000");
        rfidTag.setAuthCode(gf.format(authCode));
        rfidTag.setCommonValue(username,userId,deptId);
        boolean save = save(rfidTag);
        if (save){
            return rfidTag;
        }
        throw new ServiceException("存储失败");
    }

    /***
     * 根据识别码查询
     * @param authCode 识别码
     * @return RfidTag
     */
    @Override
    public RfidTag getRfidByTagCode(String authCode) {
        LambdaQueryWrapper<RfidTag> wrapper = Wrappers.lambdaQuery(RfidTag.class);
        wrapper.eq(RfidTag::getAuthCode,authCode);
        return getOne(wrapper);
    }

    /***
     * 修改标签
     * @param rfidTag 识别码
     * @return int
     */
    @Override
    public int updateQpRfidTag(RfidTag rfidTag) {
        return updateById(rfidTag) ?1:0;
    }

    @Override
    public List<RfidTag> list(RfidTag rfidTag) {
        LambdaQueryWrapper<RfidTag> wrapper = Wrappers.lambdaQuery();
        //时间
        String startTime = rfidTag.getStartTime();
        String endTime = rfidTag.getEndTime();
        wrapper.apply(StringUtils.hasLength(startTime), "DATE_FORMAT(create_time,'%Y-%m-%d') >=date_format('" + startTime + "','%Y-%m-%d')")
                .apply(StringUtils.hasLength(endTime), "DATE_FORMAT(create_time,'%Y-%m-%d') <=date_format('" + endTime + "','%Y-%m-%d')");

        //搜索值
        String searchValue = rfidTag.getSearchValue();
        if (StringUtils.hasLength(searchValue)) {
            wrapper.like(RfidTag::getAuthCode, searchValue)
                    .or()
                    .like(RfidTag::getTagCode, searchValue);
        }
        return list(wrapper);
    }


}
