package com.ywt.service;

import com.ywt.core.BaseResponse;
import com.ywt.core.DataResponse;
import com.ywt.core.utils.Checker;
import com.ywt.domain.entity.glasses.DictType;
import com.ywt.domain.entity.glasses.DictTypeRepository;

import com.ywt.params.mg.DictType.DictTypeVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@DubboService
@Service
public class DictTypeService extends ServiceBase {

    private static final Logger logger = LoggerFactory.getLogger(DictTypeService.class);

    @Autowired
    private DictTypeRepository dictTypeRepository;

    @Autowired
    private UserService userService;


    public List<DictType> getDictType(String typeCode) {
        return dictTypeRepository.getDictType(typeCode);
    }

    //查询类型或单位
    public BaseResponse getDictTypeList(String typeCode) {
        DataResponse<HashMap<String, Object>> response = new DataResponse<>();
        try {
            int shopId = userService.getShopId();
            List<DictType> typeList = dictTypeRepository.getDictType(typeCode, shopId);
            List<DictTypeVo> tyList = new ArrayList<>();
            if (!Checker.isNone(typeList)) {
                for (DictType ls : typeList) {
                    DictTypeVo dictTypeVo = new DictTypeVo();
                    int id = ls.getId();
                    dictTypeVo.setId(id);
                    String name = ls.getItemName();
                    dictTypeVo.setItemName(name);
                    int weight = ls.getWeight();
                    dictTypeVo.setWeight(weight);
                    tyList.add(dictTypeVo);
                }
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("list", tyList);

            response.setData(map);

        } catch (Exception e) {
            logger.error("/dictType/getDictTypeList(): {}", e.getMessage(), e);
            e.printStackTrace();
            return response.failed(BaseResponse.OTHER_ERROR, "数据读取失败");
        }
        return response.succeed("成功");

    }


    //增加或者修改种类名称
    public BaseResponse addOrUpdateDictType(int id, String itemName, String typeCode, Integer weight) {
        BaseResponse response = new BaseResponse();
        DictType dictType = null;

        if (id != 0) {
            int shopId = userService.getShopId();
            if (id < 0) {
                return response.failed(BaseResponse.PARAMETER_ERROR, "传过来的商品ID有错");
            }
            dictType = dictTypeRepository.checkTypeById(id, typeCode, shopId);
            if (Checker.isNone(dictType)) {
                return response.failed(BaseResponse.PARAMETER_ERROR, "参数错误，找不到对象");
            }
            int count = dictTypeRepository.checkItemNameById(itemName.trim(), id, shopId);

            if (count > 0) {
                return response.failed(BaseResponse.PARAMETER_ERROR, "类型名称出现重复");
            }
            dictType.setTypeCode(typeCode);

            dictType.setItemName(itemName);
            dictType.setUpdateTime(new Date());
            if (weight <= 0) {
                dictType.setWeight(0);
                dictTypeRepository.save(dictType);

            } else {
                dictType.setWeight(weight);
                dictTypeRepository.save(dictType);
            }

        } else {

            int shopId = userService.getShopId();
            int count = dictTypeRepository.checkItemName(itemName.trim(), shopId);

            if (count > 0) {

                return response.failed(BaseResponse.PARAMETER_ERROR, "该种类已存在");
            }
            dictType = new DictType();
            dictType.setTypeCode(typeCode);
            dictType.setItemName(itemName);
            dictType.setShopId(shopId);
            dictType.setCreatTime(new Date());
            dictType.setUpdateTime(new Date());

            if (Checker.isNone(weight)) {
                dictType.setWeight(0);
                dictTypeRepository.save(dictType);

            } else {
                dictType.setWeight(weight);
                dictTypeRepository.save(dictType);
            }

        }
        return response.succeed("处理成功");
    }


    //增加或者修改单位名称
    public BaseResponse addOrUpdateDictUnit(int id, String itemName, String typeCode, Integer weight) {
        BaseResponse response = new BaseResponse();
        DictType dictType = null;
        int shopId = userService.getShopId();
        if (id != 0) {
            if (id < 0) {
                return response.failed(BaseResponse.PARAMETER_ERROR, "传过来的商品ID有错");
            }
            dictType = dictTypeRepository.checkTypeById(id, typeCode, shopId);
            if (Checker.isNone(dictType)) {
                return response.failed(BaseResponse.PARAMETER_ERROR, "参数错误，找不到对象");
            }
            int count = dictTypeRepository.checkItemNameById(itemName.trim(), id, shopId);

            if (count > 0) {
                return response.failed(BaseResponse.PARAMETER_ERROR, "单位名称出现重复");
            }


            dictType.setTypeCode(typeCode);

            dictType.setItemName(itemName);
            dictType.setUpdateTime(new Date());

            dictTypeRepository.save(dictType);


        } else {
            int count = dictTypeRepository.checkItemName(itemName.trim(), shopId);

            if (count > 0) {

                return response.failed(BaseResponse.PARAMETER_ERROR, "该种类已存在");
            }
            dictType = new DictType();
            dictType.setTypeCode(typeCode);
            dictType.setItemName(itemName);
            dictType.setShopId(shopId);
            dictType.setUpdateTime(new Date());
            dictType.setCreatTime(new Date());

            dictTypeRepository.save(dictType);
        }


        return response.succeed("处理成功");
    }


}
