package com.hys.app.controller.erp.use;

import com.hys.app.framework.context.user.AdminUserContext;
import com.hys.app.framework.database.WebPage;
import com.hys.app.framework.exception.ServiceException;
import com.hys.app.model.errorcode.GoodsErrorCode;
import com.hys.app.model.goods.dos.SpecValuesDO;
import com.hys.app.model.goods.dos.SpecificationDO;
import com.hys.app.model.goods.vo.SpecificationVO;
import com.hys.app.model.support.LogClient;
import com.hys.app.model.support.validator.annotation.Log;
import com.hys.app.service.goods.SpecValuesManager;
import com.hys.app.service.goods.SpecificationManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 规格项控制器
 *
 * @author fk
 * @version v2.0
 * @since v7.0.0 2018-03-20 09:31:27
 */
@RestController
@RequestMapping("/admin/goods/specs")
@Api(description = "规格项相关API")
public class SpecificationManagerController {

    @Autowired
    private SpecificationManager specificationManager;

    @Autowired
    private SpecValuesManager specValuesManager;

    @ApiOperation(value = "查询规格项列表", response = SpecificationDO.class)
    @GetMapping
    public WebPage list(@ApiIgnore @NotEmpty(message = "页码不能为空") Long pageNo,
                        @ApiIgnore @NotEmpty(message = "每页数量不能为空") Long pageSize, String keyword) {
        return this.specificationManager.list(pageNo, pageSize, keyword);
    }

    @ApiOperation(value = "添加规格项", response = SpecificationDO.class)
    @PostMapping
    @Log(client = LogClient.admin, detail = "创建名称为[${specification.specName}]的规格项")
    public SpecificationDO add(@Valid SpecificationDO specification) {
        AdminUserContext.hasCompany();
        // 平台的规格
        specification.setSellerId(0L);
        this.specificationManager.add(specification);

        return specification;
    }

    @PutMapping(value = "/{id}")
    @ApiOperation(value = "修改规格项", response = SpecificationDO.class)
    @Log(client = LogClient.admin, detail = "修改id为[${id}]的规格项")
    public SpecificationDO edit(@Valid SpecificationDO specification, @PathVariable Long id) {
        specificationManager.edit(specification, id);
        return specification;
    }

    @DeleteMapping(value = "/{ids}")
    @ApiOperation(value = "删除规格项")
    @Log(client = LogClient.admin, detail = "删除id为[${id}]的规格项")
    public String delete(@PathVariable Long[] ids) {
        this.specificationManager.delete(ids);
        return "";
    }

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "查询一个规格项")
    public SpecificationDO get(@PathVariable Long id) {
        SpecificationDO specification = this.specificationManager.getModel(id);
        return specification;
    }

    @ApiOperation(value = "根据分类id查询规格信息(包括规格值)", notes = "根据分类id查询规格")
    @GetMapping("/categories/{category_id}")
    public List<SpecificationVO> queryAdminSpec(@PathVariable("category_id") Long categoryId) {
        return this.specificationManager.queryAdminSpec(categoryId);
    }

    @ApiOperation(value = "平台自定义某分类的规格项", response = SpecificationDO.class)
    @PostMapping("/categories/{category_id}")
    public SpecificationDO addAdminSpec(@PathVariable("category_id") Long categoryId,
                                        @ApiIgnore @NotEmpty(message = "规格名称不能为空") @Length(max = 50, message = "规格名称不能超过50个字符") String specName) {
        // 商家的规格
        SpecificationDO specification = this.specificationManager.addSellerSpec(categoryId, specName);
        return specification;
    }

    @ApiOperation(value = "平台自定义某规格的规格值")
    @PostMapping("/categories/{spec_id}/values")
    public SpecValuesDO addValue(@PathVariable("spec_id") Long specId,
                                 @ApiIgnore @NotEmpty(message = "规格值名称不能为空") @Length(max = 50, message = "规格值名称不能超过50个字符") String valueName) {
        SpecificationDO spec = specificationManager.getModel(specId);
        if (spec == null) {
            throw new ServiceException(GoodsErrorCode.E306.code(), "无权操作");
        }
        if (specificationManager.flagSellerSpec(0L, specId, valueName)) {
            throw new ServiceException(GoodsErrorCode.E306.code(), "该规格值已存在 ！");
        }
        SpecValuesDO value = new SpecValuesDO(specId, valueName, 0L);

        value.setSpecName(spec.getSpecName());

        this.specValuesManager.add(value);

        return value;
    }
}
