package io.bdty.modules.cniyv.controller;

import io.bdty.common.annotation.LogOperation;
import io.bdty.common.constant.Constant;
import io.bdty.common.page.PageData;
import io.bdty.common.utils.ExcelUtils;
import io.bdty.common.utils.Result;
import io.bdty.common.validator.AssertUtils;
import io.bdty.common.validator.ValidatorUtils;
import io.bdty.common.validator.group.AddGroup;
import io.bdty.common.validator.group.DefaultGroup;
import io.bdty.common.validator.group.UpdateGroup;
import io.bdty.modules.cniyv.dto.CniyvProductDTO;
import io.bdty.modules.cniyv.dto.CniyvProductTypeDTO;
import io.bdty.modules.cniyv.excel.CniyvProductTypeExcel;
import io.bdty.modules.cniyv.service.CniyvProductService;
import io.bdty.modules.cniyv.service.CniyvProductTypeService;
import io.bdty.modules.sys.dto.SysMenuDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.apache.poi.poifs.property.Parent;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 产品分类
 *
 * @author zxj zxj
 * @since 1.0.0 2024-03-13
 */
@RestController
@RequestMapping("cniyv/cniyvproducttype")
@Api(tags="产品分类")
public class CniyvProductTypeController {
    @Autowired
    private CniyvProductTypeService cniyvProductTypeService;
    
    @Autowired
    private CniyvProductService cniyvProductService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<PageData<CniyvProductTypeDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<CniyvProductTypeDTO> page = cniyvProductTypeService.page(params);
        for (CniyvProductTypeDTO cniyvProductTypeDTO : page.getList()) {
        	if (cniyvProductTypeDTO.getParentid()!=null) {
        		CniyvProductTypeDTO parent = cniyvProductTypeService.get(cniyvProductTypeDTO.getParentid());
        		cniyvProductTypeDTO.setParentName(parent!=null?parent.getName():"");
			}
		}
        return new Result<PageData<CniyvProductTypeDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("cniyv:cniyvproducttype:info")
    public Result<CniyvProductTypeDTO> get(@PathVariable("id") Long id){
        CniyvProductTypeDTO data = cniyvProductTypeService.get(id);
        CniyvProductTypeDTO parent = cniyvProductTypeService.get(data.getParentid());
        data.setParentName(parent!=null?parent.getName():"");
        return new Result<CniyvProductTypeDTO>().ok(data);
    }
    
    @GetMapping("info/{id}")
    @ApiOperation("详情")
    public Result<CniyvProductTypeDTO> info(@PathVariable("id") Long id){
        CniyvProductTypeDTO data = cniyvProductTypeService.get(id);
        CniyvProductTypeDTO parent = cniyvProductTypeService.get(data.getParentid());
        data.setParentName(parent!=null?parent.getName():"");
        return new Result<CniyvProductTypeDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("cniyv:cniyvproducttype:save")
    public Result save(@RequestBody CniyvProductTypeDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        cniyvProductTypeService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("cniyv:cniyvproducttype:update")
    public Result update(@RequestBody CniyvProductTypeDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        cniyvProductTypeService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("cniyv:cniyvproducttype:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        cniyvProductTypeService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("cniyv:cniyvproducttype:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<CniyvProductTypeDTO> list = cniyvProductTypeService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "产品分类", list, CniyvProductTypeExcel.class);
    }
    
    @GetMapping("list")
    @ApiOperation("列表")
    public Result<List<CniyvProductTypeDTO>> list(@ApiIgnore @RequestParam Map<String, Object> params) {
        List<CniyvProductTypeDTO> list = cniyvProductTypeService.list(params);
        return new Result<List<CniyvProductTypeDTO>>().ok(list);
    }
    
    @GetMapping("listProduct")
    @ApiOperation("产品分类含分类下产品列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String"),
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int")
    })
    public Result<List<CniyvProductTypeDTO>> listProduct(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<CniyvProductTypeDTO> page = cniyvProductTypeService.page(params);
        List<CniyvProductTypeDTO> list = page.getList();
        Map<String, Object> productParams = new HashMap<String, Object>();
        for (CniyvProductTypeDTO cniyvProductTypeDTO : list) {
        	productParams.put(Constant.PAGE, "1");
            productParams.put(Constant.ORDER_FIELD, "count");
            productParams.put(Constant.ORDER, "desc");
            productParams.put(Constant.LIMIT, "4");
            productParams.put("typeid", String.valueOf(cniyvProductTypeDTO.getPtsid()));
            PageData<CniyvProductDTO> productPage = cniyvProductService.page(productParams);
            List<CniyvProductDTO> productlist = productPage.getList();
            for (CniyvProductDTO product : productlist) {
            	if (product.getTypeid()!=null) {
            		CniyvProductTypeDTO typeDTO = cniyvProductTypeService.get(product.getTypeid());
                	product.setType(typeDTO!=null?typeDTO.getName():"");
    			}
    			
    		}
            cniyvProductTypeDTO.setProductList(productlist);
		}
        return new Result<List<CniyvProductTypeDTO>>().ok(list);
    }


}