package cn.com.ty.lift.ud.dataBank.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.com.ty.lift.ud.dataBank.mapper.entity.CategoryConstant;
import cn.com.ty.lift.ud.dataBank.mapper.entity.LiftCategoryEntity;
import cn.com.ty.lift.ud.dataBank.service.ILiftCategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.ty.lift.ud.dataBank.mapper.LiftBrandMapper;
import cn.com.ty.lift.ud.dataBank.mapper.entity.LiftBrandEntity;
import cn.com.ty.lift.ud.dataBank.mapper.entity.LiftBrandVo;
import cn.com.ty.lift.ud.dataBank.service.ILiftBrandService;

/**
 * 电梯品牌 -ServiceImpl
 *
 * @author Xson
 * @date 2020-04-23
 */
@Service
@Transactional
public class LiftBrandServiceImpl extends ServiceImpl<LiftBrandMapper, LiftBrandEntity> implements ILiftBrandService {

    @Autowired
    private LiftBrandMapper liftBrandMapper;

    @Autowired
    private ILiftCategoryService categoryService;
    @Override
    public List<LiftBrandVo> brandListApp(LiftBrandEntity lt) {
        List<LiftBrandEntity> selectList = null;
        List<LiftBrandEntity> hotList = null;
        if (StringUtils.isNotBlank(lt.getSercheKey())) {
            selectList = liftBrandMapper.selectList(new QueryWrapper<LiftBrandEntity>().like("name", lt.getSercheKey()));
            hotList = liftBrandMapper
                .selectList(new QueryWrapper<LiftBrandEntity>().like("name", lt.getSercheKey()).eq("hot_flag", 1));
        } else {
            selectList = liftBrandMapper.selectList(null);
            hotList = liftBrandMapper.getHotList();
        }
        List<LiftBrandVo> result = new ArrayList<>();

        LiftBrandVo llv = new LiftBrandVo();
        llv.setName("热门");
        llv.setListBrand(hotList);
        result.add(llv);
        for (int i = 1; i <= 26; i++) {
            LiftBrandVo lv = new LiftBrandVo();
            String upperCase = String.valueOf((char)(96 + i)).toUpperCase();
            List<LiftBrandEntity> listLetter = new ArrayList<LiftBrandEntity>();
            for (LiftBrandEntity str : selectList) {
                /*String zm = PingYinUtil.getFirstSpell(str.getName()).substring(0,1);
                if(upperCase.equals(zm)) {
                	listLetter.add(str);
                }*/
                // change by xson，通过电梯品牌code进行排序
                if (upperCase.equals(str.getCode().substring(0, 1))) {
                    listLetter.add(str);
                }
            }
            lv.setName(upperCase);
            lv.setListBrand(listLetter);
            if (!listLetter.isEmpty()) {
                result.add(lv);
            }
            // map.put(upperCase, listLetter);
        }

        // map.put("hot", hotList);
        return result;
    }

    @Override
    public void updateByIds(List<Long> ids, Integer status) {
        for (Long id : ids) {
            LiftBrandEntity bit = new LiftBrandEntity();
            bit.setId(id);
            bit.setStatuz(status);
            liftBrandMapper.updateById(bit);
        }
    }

    @Override
    public void updateHotFlagByIds(List<Long> ids, Integer status) {
        for (Long id : ids) {
            LiftBrandEntity bit = new LiftBrandEntity();
            bit.setId(id);
            bit.setHotFlag(status);
            liftBrandMapper.updateById(bit);
        }
    }

    @Override
    public boolean save(LiftBrandEntity entity) {
        super.save(entity);
        LiftCategoryEntity straightLiftEntity = LiftCategoryEntity.getStraight(entity.getId(), "直梯", "1");
        categoryService.save(straightLiftEntity);

        LiftCategoryEntity stairLiftEntity = LiftCategoryEntity.getStraight(entity.getId(), "扶梯", "2");
        categoryService.save(stairLiftEntity);

        categoryService.execuSql(CategoryConstant.getStairsLift(entity.getId(),straightLiftEntity.getId().intValue()));
        return false;
    }

    @Override
    public List<LiftBrandEntity> queryAll() {
        return liftBrandMapper.queryAll();
    }

    @Override
    public List<LiftBrandEntity> getByName(String name) {
        return liftBrandMapper.getByName(name);
    }

}
