package com.ywt.controllers.mg;


import com.ywt.core.BaseResponse;
import com.ywt.core.DataResponse;
import com.ywt.core.MGRight;
import com.ywt.core.MGRightTypeDef;
import com.ywt.core.utils.Checker;

import com.ywt.domain.entity.glasses.DictType;
import com.ywt.params.mg.DictType.DictTypeRequest;
import com.ywt.params.mg.DictType.DictTypeVo;

import com.ywt.params.mg.DictType.TypeCodeRequest;
import com.ywt.service.DictTypeService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

@RestController("/mg/dictType")
@RequestMapping({"/mg/dictType"})
@MGRight
@Api(description = "眼睛店后台商品种类列表")
public class DictTypeController {

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

    @Autowired
    private DictTypeService dictTypeService;


    //增加或修改种类或单位类型
    @MGRight(need = true, menuCode = {" /addOrUpdateDictType"}, type = MGRightTypeDef.Menu | MGRightTypeDef.Logined)
    @RequestMapping({"/addOrUpdateDictType"})
    BaseResponse addOrUpdateDictType(@RequestBody DictTypeRequest request
    ) {
        BaseResponse response = new BaseResponse();
        if (Checker.isNull(request.getItemName())) {
            return response.failed(BaseResponse.PARAMETER_ERROR, "种类名称不能为空");
        }
        try {
            if (!Checker.isNone(request.getWeight())) {
                return dictTypeService.addOrUpdateDictType(request.getId(), request.getItemName(), request.getTypeCode(), request.getWeight());
            } else {
                return dictTypeService.addOrUpdateDictType(request.getId(), request.getItemName(), request.getTypeCode(), 0);
            }

        } catch (Exception e) {
            logger.error("/mg/dictType/addOrUpdateDictType(): {}", e.getMessage(), e);
            e.printStackTrace();
            return response.failed(BaseResponse.APP_ERROR, "处理失败");
        }
    }


    //增加或修改眼镜度数类型
    @MGRight(need = true, menuCode = {" /addOrUpdateDictUnit"}, type = MGRightTypeDef.Menu | MGRightTypeDef.Logined)
    @RequestMapping({"/addOrUpdateDictUnit"})
    BaseResponse addOrUpdateDictType2(@RequestParam(value = "id", defaultValue = "0") int id,
                                      @RequestParam(value = "itemName") String itemName,
                                      @RequestParam(value = "typeCode") String typeCode,
                                      @RequestParam(name = "weight") Integer weight
    ) {
        BaseResponse response = new BaseResponse();
        if (Checker.isNull(itemName)) {
            return response.failed(BaseResponse.PARAMETER_ERROR, "单位名称不能为空");
        }
        try {
            return dictTypeService.addOrUpdateDictUnit(id, itemName, typeCode, weight);
        } catch (Exception e) {
            logger.error("/mg/dictType/addOrUpdateDictType(): {}", e.getMessage(), e);
            e.printStackTrace();
            return response.failed(BaseResponse.APP_ERROR, "处理失败");
        }
    }


    //查询种类或单位类型
    @MGRight(need = true, menuCode = {" /getDictTypeList"}, type = MGRightTypeDef.Menu | MGRightTypeDef.Logined)
    @RequestMapping({"/getDictTypeList"})
    BaseResponse getDictTypeList(@RequestBody TypeCodeRequest request) {
        return dictTypeService.getDictTypeList(request.getTypeCode());
    }

    //展示眼睛度数类型列表
    @MGRight(need = true, menuCode = {" /getDictType"}, type = MGRightTypeDef.Menu | MGRightTypeDef.Logined)
    @RequestMapping({"/getDictType"})
    BaseResponse getDictType(@RequestBody TypeCodeRequest request) {
        DataResponse<List<DictTypeVo>> response = new DataResponse<>();
        response.setCode(BaseResponse.SUCCEED);

        try {
            List<DictTypeVo> tyList = new ArrayList<>();
            List<DictType> typeList = dictTypeService.getDictType(request.getTypeCode());

            for (DictType ls : typeList) {

                DictTypeVo dictTypeVo = new DictTypeVo();
                int id = ls.getId();
                dictTypeVo.setId(id);
                String name = ls.getItemName();
                dictTypeVo.setItemName(name);
                tyList.add(dictTypeVo);
            }
            response.setData(tyList);
        } catch (Exception e) {
            logger.error("getDictType: {}", e.getMessage());
            return response.failed(BaseResponse.APP_EXCEPTION, "暂时无法获取数据，请稍后重试");
        }
        return response.succeed("处理成功");
    }
}