package cn.pluss.platform.controller.deviceGoods;


import cn.pluss.platform.DeviceGoodsService;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.device.DeviceGoodTagService;
import cn.pluss.platform.device.DeviceTypeService;
import cn.pluss.platform.entity.DeviceGoodTag;
import cn.pluss.platform.entity.DeviceGoods;
import cn.pluss.platform.entity.DeviceType;
import cn.pluss.platform.util.OssOperatUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 设备商品展示表 前端控制器
 * </p>
 *
 * @author Djh
 * @since 2020-11-11
 */
@Controller
@RequestMapping("/deviceGoods")
public class DeviceGoodsController extends BaseModelController<DeviceGoodsService, DeviceGoods> {

    @Autowired
    private DeviceTypeService deviceTypeService;

    @Autowired
    private DeviceGoodTagService deviceGoodTagService;

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/index")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView("deviceGoods/list");
        List<DeviceType> deviceTypeList = deviceTypeService.list();

        view.addObject("deviceTypeDict", deviceTypeList);
        view.addObject("deviceTypeJsonDict", JSONObject.toJSONString(deviceTypeList));
        return view;
    }

    @Override
    public Result<Page<DeviceGoods>> page(Page<DeviceGoods> page, DeviceGoods condition, OrderItem orderItem) {
        orderItem.setAsc(false).setColumn("createTime");
        Result<Page<DeviceGoods>> data = super.page(page, condition, orderItem);

        List<DeviceGoodTag> tagList = deviceGoodTagService.list();

        for (DeviceGoods item : data.getData().getRecords()) {
            for (DeviceGoodTag deviceGoodTag : tagList) {
                if (Objects.equals(item.getTagId(), deviceGoodTag.getId())) {
                    item.setTag(deviceGoodTag.getTag());
                    break;
                }
            }
        }

        return data;
    }

    /**
     * 数据回显
     */
    @GetMapping({"/detail/{id}", "/detail"})
    public String detail(Model model, @PathVariable(value = "id", required = false) Integer id) {
        List<DeviceType> deviceTypeList = deviceTypeService.list();
        model.addAttribute("deviceTypeDict", deviceTypeList);

        List<DeviceGoodTag> list = deviceGoodTagService.list();
        model.addAttribute("deviceGoodTagList", list);

        Map<String, String> uploadParam = OssOperatUtil.getUploadParam();
        model.addAttribute("uploadParam",uploadParam);
        if (id != null) {
            DeviceGoods deviceGoods = baseService.getById(id);
            for (DeviceType deviceType : deviceTypeList) {
                if (deviceGoods.getCode().equals(deviceType.getCode())) {
                    deviceGoods.setCodeName(deviceType.getName());
                    break;
                }
            }

            model.addAttribute("entity",deviceGoods);
        }
        return "deviceGoods/detail";
    }

//    /**
//     * 数据回显
//     */
//    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/detail/{id}", "/detail"})
//    @ResponseBody
//    public ModelAndView detail(@PathVariable(value = "id", required = false) Integer id) {
//        ModelAndView view = new ModelAndView("deviceGoods/detail");
//        List<Dict> deviceType = dictService.getByCode(Collections.singletonList("DEVICE_TYPE"));
//        view.addObject("deviceTypeDict", deviceType);
//        if (id != null) {
//            Map<String, String> uploadParam = OssOperatUtil.getUploadParam();
//            view.addObject("uploadParam",uploadParam);
//            DeviceGoods deviceGoods = baseService.getById(id);
//
//            for (Dict dict : deviceType) {
//                if (deviceGoods.getCode().equals(dict.getValue())) {
//                    deviceGoods.setCodeName(dict.getName());
//                    break;
//                }
//            }
//
//            view.addObject("entity",deviceGoods);
//        }else {
//            Map<String, String> uploadParam = OssOperatUtil.getUploadParam();
//            view.addObject("uploadParam",uploadParam);
//        }
//        return view;
//    }
    @PostMapping("saveOrUpdate")
    @ResponseBody
    public Result<String> saveOrUpdate(@RequestBody DeviceGoods deviceGoods) {
        baseService.saveOrUpdate(deviceGoods);
        return ResultGenerator.genSuccessResult("保存成功",null);
    }
}
