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.EquipmentBrandReq;
import com.lz.modules.equipment.entity.EquipmentType;
import com.lz.modules.equipment.entity.resp.EquipmentBrandResp;
import com.lz.modules.equipment.service.EquipmentBrandService;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 品牌表
 *
 * @author quyixiao
 * @version 1.0
 * @since 2020年07月29日 16时48分22秒
 */
@RestController
public class EquipmentBrandController {


    @Resource
    private EquipmentBrandService equipmentBrandService;

    @Autowired
    private IEquipmentTypeService equipmentTypeService;

    @Resource
    private IEquipmentInfoService equipmentInfoService;

    /**
     * 添加品牌表页面
     *
     * @return 添加页面
     * @throws Exception 异常
     */
    @RequestMapping("/modules/brand/equipmentBrandAddPage")
    public R equipmentBrandAddPage() throws Exception {
        List<EquipmentType> equipmentTypes = equipmentTypeService.selectAll();
        return R.ok().put("types",equipmentTypes);
    }

    /**
     * 添加品牌表
     *
     * @throws Exception 异常
     */
    @RequestMapping("/modules/brand/equipmentBrandAdd")
    @RequiresPermissions("user:device:brand:save")
    public R equipmentBrandAdd(@RequestBody EquipmentBrandReq req) throws Exception {
        EquipmentBrand equipmentBrand = new EquipmentBrand();
        BeanUtils.copyProperty(equipmentBrand, req, true);
        equipmentBrandService.insertEquipmentBrand(equipmentBrand);
        return R.ok("添加成功");
    }

    /**
     * 更新品牌表页面
     *
     * @return 返回页面
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/brand/equipmentBrandEditPage")
    public R equipmentBrandEditPage(@RequestBody EquipmentBrandReq req) throws Exception {
        EquipmentBrand equipmentBrand = equipmentBrandService.selectEquipmentBrandById(req.getId());
        List<EquipmentType> equipmentTypes = equipmentTypeService.selectAll();
        return R.ok().put("equipmentBrand", equipmentBrand)
                .put("types",equipmentTypes);
    }


    /**
     * 更新品牌表
     *
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/brand/equipmentBrandEdit")
    @RequiresPermissions("user:device:brand:update")
    public R equipmentBrandEdit(@RequestBody EquipmentBrandReq req) throws Exception {
        EquipmentBrand equipmentBrand = equipmentBrandService.selectEquipmentBrandById(req.getId());
        BeanUtils.copyProperty(equipmentBrand, req, true);
        equipmentBrandService.updateEquipmentBrandById(equipmentBrand);
        equipmentInfoService.updateCoverEquipmentInfoByBrandId(equipmentBrand.getId(), equipmentBrand.getBrand());
        return R.ok("编辑成功");
    }

    /**
     * 删除品牌表
     *
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/brand/equipmentBrandDelete")
    @RequiresPermissions("user:device:brand:delete")
    public R equipmentBrandDelete(@RequestBody EquipmentBrandReq req) throws Exception {
        equipmentBrandService.deleteEquipmentBrandById(req.getId());
        return R.ok("删除成功");
    }

    /**
     * 获取品牌表集合
     *
     * @return 返回页面
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/brand/equipmentBrandManager")
    public R equipmentBrandManager() throws Exception {
        List<EquipmentType> equipmentTypes = equipmentTypeService.selectAll();
        return R.ok()
                .put("types",equipmentTypes);
    }
    /**
     * 获取品牌表集合
     *
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/brand/equipmentBrandList")
    @RequiresPermissions("user:device:brand:list")
    public R equipmentBrandList(@RequestBody EquipmentBrandReq req) throws Exception {
        PageUtils page = equipmentBrandService.selectByCondition(req);

        List<EquipmentType> equipmentTypes = equipmentTypeService.selectAll();
        Map<Long, EquipmentType> map = new HashMap<>();
        for(EquipmentType type : equipmentTypes){
            map.put(type.getId(),type);
        }
        List<EquipmentBrand> equipmentBrandList = page.getList();
        List<EquipmentBrandResp> resps = new ArrayList<>();
        for(EquipmentBrand brand : equipmentBrandList){
            EquipmentBrandResp resp = new EquipmentBrandResp();
            resp.setBrand(brand.getBrand());
            resp.setId(brand.getId());
            resp.setRank(brand.getRank());
            resp.setTypeId(brand.getTypeId());
            EquipmentType type = map.get(brand.getTypeId());
            if(type !=null){
                resp.setTypeName(type.getType());
            }else{
                resp.setTypeName("【分类被删】");
            }
            resps.add(resp);
        }
        EquipmentType type = new EquipmentType();
        type.setId(null);
        type.setType("全部");
        equipmentTypes.add(0, type);
        return R.ok().put("total", page.getTotalCount())
                .put("rows", resps)
                .put("types", equipmentTypes)
              ;

    }
}
