package com.lz.modules.equipment.controller;

import com.lz.common.utils.PageUtils;
import com.lz.common.utils.R;
import com.lz.modules.app.utils.BeanUtils;
import com.lz.modules.equipment.entity.EquipmentBrand;
import com.lz.modules.equipment.entity.EquipmentSpecs;
import com.lz.modules.equipment.entity.EquipmentSpecsReq;
import com.lz.modules.equipment.entity.EquipmentType;
import com.lz.modules.equipment.entity.resp.EquipmentSpecsResp;
import com.lz.modules.equipment.service.EquipmentBrandService;
import com.lz.modules.equipment.service.EquipmentSpecsService;
import com.lz.modules.equipment.service.IEquipmentInfoService;
import com.lz.modules.equipment.service.IEquipmentTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
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.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * 规格表
 *
 * @author quyixiao
 * @version 1.0
 * @since 2020年07月29日 16时48分23秒
 */
@RestController
public class EquipmentSpecsController {


    @Resource
    private EquipmentSpecsService equipmentSpecsService;

    @Autowired
    private IEquipmentTypeService equipmentTypeService;

    @Autowired
    private EquipmentBrandService equipmentBrandService;

    @Resource
    private IEquipmentInfoService equipmentInfoService;

    /**
     * 添加规格表页面
     *
     * @return 添加页面
     * @throws Exception 异常
     */
    @RequestMapping("/modules/specs/equipmentSpecsAddPage")
    public R equipmentSpecsAddPage() throws Exception {
        List<EquipmentType> types = equipmentTypeService.selectAll();
        List<EquipmentBrand> brands = equipmentBrandService.selectByTypeId(types.get(0).getId());
        return R.ok().put("types",types)
                .put("brands",brands);
    }

    /**
     * 添加规格表
     *
     * @throws Exception 异常
     */
    @RequestMapping("/modules/specs/equipmentSpecsAdd")
    @RequiresPermissions("user:device:specs:save")
    public R equipmentSpecsAdd(@RequestBody EquipmentSpecsReq req) throws Exception {
        EquipmentSpecs equipmentSpecs = new EquipmentSpecs();
        BeanUtils.copyProperty(equipmentSpecs, req, true);
        equipmentSpecsService.insertEquipmentSpecs(equipmentSpecs);
        return R.ok("添加成功");
    }

    /**
     * 更新规格表页面
     *
     * @return 返回页面
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/specs/equipmentSpecsEditPage")
    public R equipmentSpecsEditPage(@RequestBody EquipmentSpecsReq req) throws Exception {
        EquipmentSpecs equipmentSpecs = equipmentSpecsService.selectEquipmentSpecsById(req.getId());
        List<EquipmentType> types = equipmentTypeService.selectAll();
        List<EquipmentBrand> brands = equipmentBrandService.selectByTypeId(equipmentSpecs.getTypeId());
        return R.ok().put("equipmentSpecs", equipmentSpecs)
                .put("types",types)
                .put("brands",brands);
    }

    /**
     * 更新规格表
     *
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/specs/equipmentSpecsEdit")
    @RequiresPermissions("user:device:specs:update")
    public R equipmentSpecsEdit(@RequestBody EquipmentSpecsReq req) throws Exception {
        EquipmentSpecs equipmentSpecs = equipmentSpecsService.selectEquipmentSpecsById(req.getId());
        BeanUtils.copyProperty(equipmentSpecs, req, true);
        equipmentSpecs.setGmtCreate(equipmentSpecs.getGmtCreate());
        equipmentSpecs.setGmtModified(new Date());
        equipmentSpecsService.updateEquipmentSpecsById(equipmentSpecs);
        equipmentInfoService.updateCoverEquipmentInfoBySpecsId(equipmentSpecs.getId(), equipmentSpecs.getSpecs());
        return R.ok("编辑成功");
    }

    /**
     * 删除规格表
     *
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/specs/equipmentSpecsDelete")
    @RequiresPermissions("user:device:specs:delete")
    public R equipmentSpecsDelete(@RequestBody EquipmentSpecsReq req) throws Exception {
        equipmentSpecsService.deleteEquipmentSpecsById(req.getId());
        return R.ok("删除成功");
    }

    /**
     * 获取规格表集合
     *
     * @return 返回页面
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/specs/equipmentSpecsManager")
    public R equipmentSpecsManager() throws Exception {
        List<EquipmentType> types = equipmentTypeService.selectAll();
        return R.ok().put("types",types);
    }

    /**
     * 获取规格表集合
     *
     * @return 返回页面
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/specs/brandList")
    public R brandList(@RequestBody EquipmentSpecsReq req) throws Exception {
        List<EquipmentBrand> brands = equipmentBrandService.selectByTypeId(req.getTypeId());
        EquipmentBrand band = new EquipmentBrand();
        band.setId(null);
        band.setTypeId(null);
        band.setBrand("全部");
        brands.add(0, band);
        return R.ok().put("brands",brands);
    }


    /**
     * 获取规格表集合
     *
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/specs/equipmentSpecsList")
    @RequiresPermissions("user:device:specs:list")
    public R equipmentSpecsList(@RequestBody EquipmentSpecsReq req) throws Exception {
        PageUtils page = equipmentSpecsService.selectByCondition(req);
        List<EquipmentSpecs> specsList = page.getList();
        List<EquipmentType> types = equipmentTypeService.selectAll();
        Map<Long, EquipmentType> typeMap = new HashMap<>();
        Map<Long, EquipmentBrand> brandMap = new HashMap<>();
        for (EquipmentType equipmentType : types) {
            typeMap.put(equipmentType.getId(), equipmentType);
        }
        List<EquipmentBrand> brands = equipmentBrandService.selectAll();
        for (EquipmentBrand equipmentBrand : brands) {
            brandMap.put(equipmentBrand.getId(), equipmentBrand);
        }

        List<EquipmentSpecsResp> resps = new ArrayList<>();
        for (EquipmentSpecs specs : specsList) {
            EquipmentSpecsResp resp = new EquipmentSpecsResp();
            resp.setId(specs.getId());
            EquipmentType type = typeMap.get(specs.getTypeId());
            if(type == null){
                resp.setType("【分类被删】");
            }else{
                resp.setType(type.getType());
                resp.setTypeId(type.getId());
            }

            EquipmentBrand brand = brandMap.get(specs.getBrandId());
            if(brand == null){
                resp.setBrand("【品牌被删】");
            }else{
                resp.setBrand(brand.getBrand());
                resp.setBrandId(brand.getId());
            }

            resp.setSpecs(specs.getSpecs());
            resp.setRank(specs.getRank());
            resp.setSkus(specs.getSkus());
            resp.setUsed(specs.getUsed());
            resp.setRemark(specs.getRemark());
            resps.add(resp);
        }
        EquipmentType type = new EquipmentType();
        type.setId(null);
        type.setType("全部");
        types.add(0, type);
        return R.ok().put("total", page.getTotalCount())
                .put("rows", resps)
                .put("types", types);
    }
}
