package com.mvc.service.dic;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.DicConfigAuthMapper;
import com.mvc.entity.DicConfigAuth;
import com.mvc.entity.DicConfigAuthExample;
import com.mvc.model.DicConfigAuthModel;
import com.mvc.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.*;

@Service
@Transactional
public class DicConfigAuthService {

    @Resource
    private DicConfigAuthMapper dicConfigAuthMapper;

    public Map<String, Object> findAllDicConfigAuth(DicConfigAuthModel queryModel) {
        Map<String, Object> resMap = new HashMap<>();
        Page<Object> pageObject = PageHelper.startPage(queryModel.getPage(), queryModel.getRows());

        DicConfigAuthExample example=new DicConfigAuthExample();
        DicConfigAuthExample.Criteria criteria=example.createCriteria();
        if(StringUtils.isNotEmpty(queryModel.getAuthName())){
            criteria.andAuthNameLike(StringUtil.getLikeFormat(queryModel.getAuthName()));
        }

        List<DicConfigAuth> dicConfigAuths = dicConfigAuthMapper.selectByExample(example);
        List<DicConfigAuthModel> resList = new ArrayList<>();
        for (DicConfigAuth dicConfigAuth : dicConfigAuths) {
            DicConfigAuthModel model = new DicConfigAuthModel();
            BeanUtils.copyProperties(dicConfigAuth, model);
            resList.add(model);
        }
        resMap.put("total", pageObject.getTotal());
        resMap.put("list", resList);
        return resMap;
    }

    public DicConfigAuthModel saveDicConfigAuth(DicConfigAuthModel model) throws IllegalAccessException {
        model.setAuthId(StringUtil.getUuid());
        model.setCreateTime(new Date());

        //下面是反射赋值,防止字段值为空导致APP出错 2022-07-04 guozhao
        Field[] fields =  model.getClass().getDeclaredFields();
        for( int i = 0; i < fields.length; i++){
            Field f = fields[i];
            f.setAccessible(true);
            var value=f.get(model);
            if(value==null&&f.getType()==String.class){
                f.set(model,"0");
            }
        }

        //有些项合并了,用一个字段即可,此处统一是防止APP或WEB使用的字段不一致 2022-06-29 guozhao
        model.setUnitType(model.getDigitNum());
        model.setWheelNum(model.getDigitNum());
        model.setPwmDuty(model.getPwmHz());

        DicConfigAuth dicConfigAuth = new DicConfigAuth();
        BeanUtils.copyProperties(model, dicConfigAuth);
        int insert = dicConfigAuthMapper.insert(dicConfigAuth);
        model.setId(dicConfigAuth.getId());
        return model;
    }

    public DicConfigAuthModel updateDicConfigAuth(DicConfigAuthModel model) {
        //有些项合并了,用一个字段即可,此处统一是防止APP或WEB使用的字段不一致 2022-06-29 guozhao
        model.setUnitType(model.getDigitNum());
        model.setWheelNum(model.getDigitNum());
        model.setPwmDuty(model.getPwmHz());
        DicConfigAuth dicConfigAuth = dicConfigAuthMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, dicConfigAuth);
        int i = dicConfigAuthMapper.updateByPrimaryKey(dicConfigAuth);
        return model;
    }

    public DicConfigAuthModel findDicConfigAuthByAuthId(String authid) {
        DicConfigAuthModel model = new DicConfigAuthModel();
        DicConfigAuthExample example = new DicConfigAuthExample();
        DicConfigAuthExample.Criteria criteria = example.createCriteria();
        criteria.andAuthIdEqualTo(authid);
        List<DicConfigAuth> dicConfigAuths = dicConfigAuthMapper.selectByExample(example);
        if(dicConfigAuths==null||dicConfigAuths.size()==0) return null;
        BeanUtils.copyProperties(dicConfigAuths.get(0), model);
        return model;
    }

    public int deleteDicConfigAuth(int id) {
        DicConfigAuth dicConfigAuth = dicConfigAuthMapper.selectByPrimaryKey(id);
        int i = dicConfigAuthMapper.deleteByPrimaryKey(id);
        return i;
    }
}
