package cn.pluss.platform.controller.deviceDetail;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.device.DeviceSpecService;
import cn.pluss.platform.device.DeviceStockGroupService;
import cn.pluss.platform.device.DeviceTypeService;
import cn.pluss.platform.deviceDetail.DeviceDetailService;
import cn.pluss.platform.dict.DictService;
import cn.pluss.platform.entity.*;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * [设备展示表] 前端控制器
 * </p>
 *
 * @author bzg
 * @since 2020-11-12
 */
@Controller
@RequestMapping("/deviceDetail")
public class DeviceDetailController extends BaseModelController<DeviceDetailService, DeviceDetail> {

    @Setter(onMethod_ = {@Autowired})
    private DictService dictService;

    @Autowired
    private DeviceSpecService deviceSpecService;

    @Autowired
    private DeviceTypeService deviceTypeService;

    @Autowired
    private DeviceStockGroupService deviceStockGroupService;

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/index")
    public String index(HttpServletRequest request) {
        List<Dict> deviceType = dictService.getByCode(Collections.singletonList("DEVICE_TYPE"));
        request.setAttribute("deviceTypeDict", deviceType);
        return "deviceDetail/list";
    }

    @Override
    public Result<Page<DeviceDetail>> page(Page<DeviceDetail> page, DeviceDetail condition, OrderItem orderItem) {
        orderItem.setAsc(true).setColumn("seq");
        condition.setSize(null);
        Result<Page<DeviceDetail>> pageData = super.page(page, condition, orderItem);

        List<DeviceStockGroup> deviceStockGroupList = deviceStockGroupService.list();
        List<DeviceType> deviceTypeList = deviceTypeService.list();
        List<DeviceSpec> deviceSpecList = deviceSpecService.list();

        for (DeviceDetail item : pageData.getData().getRecords()) {
            for (DeviceStockGroup deviceStockGroup : deviceStockGroupList) {
                if (deviceStockGroup.getGroupCode().equals(item.getGroupNo())) {
                    item.setGroupNoDesc(deviceStockGroup.getGroupName());
                }
            }

            for (DeviceType deviceType : deviceTypeList) {
                if (deviceType.getCode().equals(item.getCode())) {
                    item.setCodeDesc(deviceType.getName());
                }
            }

            for (DeviceSpec deviceSpec : deviceSpecList) {
                if (deviceSpec.getCode().equals(item.getCode()) && deviceSpec.getSpec().equals(item.getType())) {
                    item.setTypeDesc(deviceSpec.getSpecName());
                }
            }
        }

        return pageData;
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/detail/{id}", "/detail"})
    public String detail(HttpServletRequest request, @PathVariable(value = "id", required = false) Integer id) {
        List<DeviceType> deviceTypeDict = deviceTypeService.list();
        request.setAttribute("deviceTypeDict",deviceTypeDict);
        List<DeviceStockGroup> deviceGroupDict = deviceStockGroupService.list();
        request.setAttribute("deviceGroupDict",deviceGroupDict);
        if (id != null) {
            DeviceDetail deviceDetail = this.baseService.getById(id);
            request.setAttribute("entity",deviceDetail);
            List<DeviceSpec> subDeviceType = deviceSpecService.listByCode(deviceDetail.getCode());
            request.setAttribute("subDeviceType",subDeviceType);
        }

        return "deviceDetail/detail";
    }


    @GetMapping("/getDeviceSubVersionType/{dataType}")
    @ResponseBody
    public Result<Object> detail(@PathVariable(value = "dataType") String dataType) {
        List<DeviceSpec> deviceSpecs = deviceSpecService.listByCode(dataType);
        return ResultGenerator.genSuccessResult("获取成功",deviceSpecs);
    }

    @GetMapping("/getDeviceType/{dataType}")
    @ResponseBody
    public Result<Object> getDeviceType(@PathVariable(value = "dataType") String dataType) {
        return ResultGenerator.genSuccessResult("获取成功",null);
    }

    @PostMapping("saveOrUpdate")
    @ResponseBody
    public Result<String> saveOrUpdate(@RequestBody DeviceDetail deviceDetail) {
        if (deviceDetail.getWholesalePrice() == null) {
            deviceDetail.setWholesalePrice(deviceDetail.getUnitPrice());
            deviceDetail.setWholesaleMinNum(1);
        }

        baseService.saveOrUpdate(deviceDetail);

        return ResultGenerator.genSuccessResult("保存成功",null);
    }

}
